-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython.py
More file actions
86 lines (54 loc) · 2.57 KB
/
Python.py
File metadata and controls
86 lines (54 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
"""Untitled8.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1hP1JykoHj5Jq7eTScxDHYfmk9_QjtROn
"""
BASIC MATH OPERATIONS
# Use the + sign to perform addition:
20 + 5
# Use the - sign to perform subtraction:
20 - 5
# Use * for multiplication:
45 * 9
# Use / for division:
13 / 8
# Use // for floor division (Round decimal remainders down)
13 // 8
# Use ** for exponentiation:
5 ** 3
"""Math expressions in Python follow the normal arithmetic order of operations so * and / are executed before + and - and ** is executed before multiplication and division.
Note: Lines within Python code that begin with "#" are text comments. Comments are typically used to make notes and describe what code does and do not affect the code when it is run.
"""
# These operations are executed in reverse order of appearance due to the order of operations
4 + 2 * 7 ** 3
"""You can use parentheses in your math expressions to ensure that operations are carried out on the correct order. Operations within parentheses are carried out before operations that are external to the parentheses."""
# This time, the addition comes first and the exponentiation come last.
((5 + 2) * 4) ** 3
# The module produces the remainder you'd get when dividing two numbers. Use the % sign to take the modules in Python:
200 % 3
"""Beyond symbolic operators, Python contains a variety of named math functions available in the "math" module. To load a library into Python, "import" followed by the name of the library. """
# Load the math module
import math
# math.log() takes the natural logorithm of its argument:
math.log(5.44)
# Add a second argument to specify the log base:
math.log(100, 10) # Take the log base 10 of 100
# math.exp() raises e to the power of its argument
math.exp(50)
# Use math.sqrt() to take the square root of a number:
math.sqrt(81)
# Use abs() to get the absolute value of a number.
abs(-50)
math.pi # Get the constant pi
"""Base Python contains a round() function that lets you round numbers to the nearest whole number. You can also round up or down with math.ceil and math.floor respectively."""
# Use round() to round a number to the nearest whole number:
round(578.437)
# Add a second argument to round to a specified decimal place
round(578.437, 2) # round to 2 decimal place
# Enter a negative number to round to the left of the decimal
round(578.437, -1) # roud to the 10's place
# Round down to the nearest whole number with math.floor()
math.floor(6.8)
# Round up with math.ceil()
math.ceil(6.8)