30-Days-Of-Python
124 строки · 4.4 Кб
1# Arithmetic Operations in Python
2# Integers
3
4print('Addition: ', 1 + 2)
5print('Subtraction: ', 2 - 1)
6print('Multiplication: ', 2 * 3)
7print ('Division: ', 4 / 2) # Division in python gives floating number
8print('Division: ', 6 / 2)
9print('Division: ', 7 / 2)
10print('Division without the remainder: ', 7 // 2) # gives without the floating number or without the remaining
11print('Modulus: ', 3 % 2) # Gives the remainder
12print ('Division without the remainder: ', 7 // 3)
13print('Exponential: ', 3 ** 2) # it means 3 * 3
14
15# Floating numbers
16print('Floating Number,PI', 3.14)
17print('Floating Number, gravity', 9.81)
18
19# Complex numbers
20print('Complex number: ', 1 + 1j)
21print('Multiplying complex number: ',(1 + 1j) * (1-1j))
22
23# Declaring the variable at the top first
24
25a = 3 # a is a variable name and 3 is an integer data type
26b = 2 # b is a variable name and 3 is an integer data type
27
28# Arithmetic operations and assigning the result to a variable
29total = a + b
30diff = a - b
31product = a * b
32division = a / b
33remainder = a % b
34floor_division = a // b
35exponential = a ** b
36
37# I should have used sum instead of total but sum is a built-in function try to avoid overriding builtin functions
38print(total) # if you don't label your print with some string, you never know from where is the result is coming
39print('a + b = ', total)
40print('a - b = ', diff)
41print('a * b = ', product)
42print('a / b = ', division)
43print('a % b = ', remainder)
44print('a // b = ', floor_division)
45print('a ** b = ', exponential)
46
47# Declaring values and organizing them together
48num_one = 3
49num_two = 4
50
51# Arithmetic operations
52total = num_one + num_two
53diff = num_two - num_one
54product = num_one * num_two
55div = num_two / num_two
56remainder = num_two % num_one
57
58# Printing values with label
59print('total: ', total)
60print('difference: ', diff)
61print('product: ', product)
62print('division: ', div)
63print('remainder: ', remainder)
64
65
66# Calculating area of a circle
67radius = 10 # radius of a circle
68area_of_circle = 3.14 * radius ** 2 # two * sign means exponent or power
69print('Area of a circle:', area_of_circle)
70
71# Calculating area of a rectangle
72length = 10
73width = 20
74area_of_rectangle = length * width
75print('Area of rectangle:', area_of_rectangle)
76
77# Calculating a weight of an object
78mass = 75
79gravity = 9.81
80weight = mass * gravity
81print(weight, 'N')
82
83print(3 > 2) # True, because 3 is greater than 2
84print(3 >= 2) # True, because 3 is greater than 2
85print(3 < 2) # False, because 3 is greater than 2
86print(2 < 3) # True, because 2 is less than 3
87print(2 <= 3) # True, because 2 is less than 3
88print(3 == 2) # False, because 3 is not equal to 2
89print(3 != 2) # True, because 3 is not equal to 2
90print(len('mango') == len('avocado')) # False
91print(len('mango') != len('avocado')) # True
92print(len('mango') < len('avocado')) # True
93print(len('milk') != len('meat')) # False
94print(len('milk') == len('meat')) # True
95print(len('tomato') == len('potato')) # True
96print(len('python') > len('dragon')) # False
97
98# Boolean comparison
99print('True == True: ', True == True)
100print('True == False: ', True == False)
101print('False == False:', False == False)
102print('True and True: ', True and True)
103print('True or False:', True or False)
104
105# Another way comparison
106print('1 is 1', 1 is 1) # True - because the data values are the same
107print('1 is not 2', 1 is not 2) # True - because 1 is not 2
108print('A in Asabeneh', 'A' in 'Asabeneh') # True - A found in the string
109print('B in Asabeneh', 'B' in 'Asabeneh') # False -there is no uppercase B
110print('coding' in 'coding for all') # True - because coding for all has the word coding
111print('a in an:', 'a' in 'an') # True
112print('4 is 2 ** 2:', 4 is 2 ** 2) # True
113
114print(3 > 2 and 4 > 3) # True - because both statements are true
115print(3 > 2 and 4 < 3) # False - because the second statement is false
116print(3 < 2 and 4 < 3) # False - because both statements are false
117print(3 > 2 or 4 > 3) # True - because both statements are true
118print(3 > 2 or 4 < 3) # True - because one of the statement is true
119print(3 < 2 or 4 < 3) # False - because both statements are false
120print(not 3 > 2) # False - because 3 > 2 is true, then not True gives False
121print(not True) # False - Negation, the not operator turns true to false
122print(not False) # True
123print(not not True) # True
124print(not not False) # False