-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_number_functions.py
More file actions
272 lines (238 loc) · 11.6 KB
/
Copy path07_number_functions.py
File metadata and controls
272 lines (238 loc) · 11.6 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#======================================================================================
# NUMBER FUNCTIONS
#--------------------------------------------------------------------------------------
# Numeric Data Types in Python are used to store numbers and perform mathematical operations.
# Python has mainly 3 numeric data types.
# 1) Integer --whole numbers, no decimal points
# 2) Float --numbers with decimal points
# 3) Complex --numbers with real + imaginary parts
#======================================================================================
#======================================================================================
# 1) Type()
#--------------------------------------------------------------------------------------
# a) type() --built-in function
# b) int() --built-in function / output: int
# c) float() --built-in function / output: float
# d) complex() --built-in function / output: complex
#======================================================================================
#--------------------------------------------------------------------------------------
# a) type() --returns the data type of a value.
#--------------------------------------------------------------------------------------
x = 5
y = 5.7
z = 2 + 3j
print(type(x))
print(type(y))
print(type(z))
#--------------------------------------------------------------------------------------
# b) int(value) --converts compatible value into int value.
#--------------------------------------------------------------------------------------
# converting string value to int
x = "24"
print(type(x), x*3)
x = int(x)
print(type(x), x*3)
# converting float value to int
x = 3.14
print(type(x), x)
x = int(x)
print(type(x), x)
#--------------------------------------------------------------------------------------
# c) float(value) --converts compatible value into float value.
#--------------------------------------------------------------------------------------
# converting string to float
x = "2.35"
print(type(x), x)
x = float(x)
print(type(x), x)
# or
print(float(x))
# converting int to float
x = 30
print(type(x), x)
x = float(x)
print(type(x), x)
#--------------------------------------------------------------------------------------
# d) complex(real, imag) --creates a number using real and imaginary parts.
#--------------------------------------------------------------------------------------
x = 3 # real
y = 4 # imaginary
z = complex(x, y)
print(type(z), z)
#======================================================================================
# 2) Math Operators
#--------------------------------------------------------------------------------------
# a) 3 + 2 --operator / output: int
# b) 3 - 2 --operator / output: int
# c) 3 * 2 --operator / output: int
# d) 3 // 2 --operator / output: int
# e) 2 % 2 --operator / output: int(1 or 0)
# f) 3 ** 2 --operator / output: int
#======================================================================================
#--------------------------------------------------------------------------------------
# a) 3 + 2 --addition.
#--------------------------------------------------------------------------------------
print(2+3)
#--------------------------------------------------------------------------------------
# b) 3 - 2 --subtraction.
#--------------------------------------------------------------------------------------
print(3-2)
#--------------------------------------------------------------------------------------
# c) 3 * 2 --multiplication.
#--------------------------------------------------------------------------------------
print(3*2)
#--------------------------------------------------------------------------------------
# d) 3 / 2 --division(always gives float).
#--------------------------------------------------------------------------------------
print(3/2)
#--------------------------------------------------------------------------------------
# e) 3 // 2 --floor division(it divides two numbers and rounds down).
#--------------------------------------------------------------------------------------
print(3//2)
#--------------------------------------------------------------------------------------
# f) 3 % 2 --remainder(The leftover part after division)--used to check if a number is even.
#--------------------------------------------------------------------------------------
print(3 % 2)
#--------------------------------------------------------------------------------------
# g) 3 ** 2 --exponentiation(it raises a number to the power of another number).
#--------------------------------------------------------------------------------------
print(3**2)
#--------------------------------------------------------------------------------------
# basic mathematical shortcuts
#--------------------------------------------------------------------------------------
#--------------------------------------------
# short cut | # meaning |
#--------------------------------------------
# += | add and assign |
# -= | multiply and assign |
# /= | divide and assign |
# //= | floor divide and assign |
# %= | modulus and assign |
# **= | power and assign |
#--------------------------------------------
x = 10
x += 5 # x = x +5
print(x)
x -= 5 # x = x - 5
print(x)
x *= 3 # x = x * 3
print(x)
x /= 2 # x = x / 2
print(x)
x //= 7 # x = x // 7
print(x)
x **= 3 # x = x ** 3
print(x)
#======================================================================================
# 3) Rounding
#--------------------------------------------------------------------------------------
# a) abs() --built-in function / output: int
# b)round() --built-in function / output: int or float
#--------------------------------------------------------------------------------------
# math(module)
#--------------------------------------------------------------------------------------
# c)math.ceil() --math function / output: int
# d)math.floor() --math function / output: int
# e)math.trunc() --math function / output: int
#======================================================================================
#--------------------------------------------------------------------------------------
# a)abs()--returns the absolute(non-negative) value of a number.
#--------------------------------------------------------------------------------------
# measure distance
print(2-10)
# useful for measuring distance or size, regardless of direction.
print(abs(2-10))
#--------------------------------------------------------------------------------------
# b) round()--rounds a number to the nearest whole number up or down depending on
# what's closer
#--------------------------------------------------------------------------------------
price = 35.5428359
print(round(price))
#--------------------------------------------------------------------------------------
# round(number, n digits)--rounds the number to the specified number of decimal places
#--------------------------------------------------------------------------------------
print(round(price, 1))
print(round(price, 2))
#--------------------------------------------------------------------------------------
# floor(),ceil() are not built-in functions, it belongs to math module - import it before using.
# c) math.floor(x)--rounds a number down to the floor
#--------------------------------------------------------------------------------------
import math
print(math.floor(price))
#--------------------------------------------------------------------------------------
# d) math.ceil(x)--rounds a number up to the ceiling
#--------------------------------------------------------------------------------------
print(math.ceil(price))
#--------------------------------------------------------------------------------------
# e) math.trunc(x)--cuts off the decimal part and keeps the whole number (no rounding).
#--------------------------------------------------------------------------------------
print(math.trunc(price))
#--------------------------------------------------------------------------------------
# other math functions
#--------------------------------------------------------------------------------------
print(math.factorial(5)) # 120
print(math.pow(2,4)) # 16
print(math.fabs(-20)) # 20
print(math.gcd(12, 18)) # 6
print(math.lcm(12, 18)) # 36
#======================================================================================
# 4)random
#--------------------------------------------------------------------------------------
# a)random() --random function / output: float
# b)randint() --random function / output: int
#======================================================================================
import random
#--------------------------------------------------------------------------------------
# a) random.random()-- returns a random float between 0.0 and 1.0
#--------------------------------------------------------------------------------------
print(random.random())
#--------------------------------------------------------------------------------------
# b) random.randint(start, end)--gets a random whole number from start to end
# (both included)
#--------------------------------------------------------------------------------------
print(random.randint(100, 110))
#======================================================================================
# 5) validation
## -------------------------------------------------------------------------------------
# a) is_integer() --float class method / output: bool
# b) isinstance() --built-in function / output: bool
#======================================================================================
#--------------------------------------------------------------------------------------
# a) is_integer()--checks if a float has no decimal part(is whole number).
#--------------------------------------------------------------------------------------
x = 2.0
print(x.is_integer())
y = 10.20
print(y.is_integer())
#--------------------------------------------------------------------------------------
# b) isinstance(value, type)--checks if a value belongs to certain data type.
#--------------------------------------------------------------------------------------
z = "Hello"
print(isinstance(z, str))
print(isinstance(z, int))
k = 20.60
print(isinstance(k, float))
print(isinstance(k, bool))
#--------------------------------------------------------------------------------------
# python challenge
# Generate a random integer between 1 and 100, and check if the result is an even number.
#--------------------------------------------------------------------------------------
a = random.randint(1, 100)
if a % 2 == 0:
print(f"{a} is a even number")
else:
print(f"{a} is a odd number")
#==========================================================================================
# A module in python is a file that contains python code(functions, variables, classes)
# which you can reuse in other programs.
# Python has Built-in modules(comes with python)--no installation needed.
#==========================================================================================
#--------------------------------------------
# module | Use |
#--------------------------------------------
# math | mathematical operations |
# datetime | date and time |
# random | random numbers |
# os | file and system operations |
# sys | system-related tasks |
#--------------------------------------------