30-Days-Of-Python

Форк
0
124 строки · 4.4 Кб
1
# Arithmetic Operations in Python
2
# Integers
3

4
print('Addition: ', 1 + 2)
5
print('Subtraction: ', 2 - 1)
6
print('Multiplication: ', 2 * 3)
7
print ('Division: ', 4 / 2)                         # Division in python gives floating number
8
print('Division: ', 6 / 2)
9
print('Division: ', 7 / 2)
10
print('Division without the remainder: ', 7 // 2)   # gives without the floating number or without the remaining
11
print('Modulus: ', 3 % 2)                           # Gives the remainder
12
print ('Division without the remainder: ', 7 // 3)
13
print('Exponential: ', 3 ** 2)                     # it means 3 * 3
14

15
# Floating numbers
16
print('Floating Number,PI', 3.14)
17
print('Floating Number, gravity', 9.81)
18

19
# Complex numbers
20
print('Complex number: ', 1 + 1j)
21
print('Multiplying complex number: ',(1 + 1j) * (1-1j))
22

23
# Declaring the variable at the top first
24

25
a = 3 # a is a variable name and 3 is an integer data type
26
b = 2 # b is a variable name and 3 is an integer data type
27

28
# Arithmetic operations and assigning the result to a variable
29
total = a + b
30
diff = a - b
31
product = a * b
32
division = a / b
33
remainder = a % b
34
floor_division = a // b
35
exponential = 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
38
print(total) # if you don't label your print with some string, you never know from where is  the result is coming
39
print('a + b = ', total)
40
print('a - b = ', diff)
41
print('a * b = ', product)
42
print('a / b = ', division)
43
print('a % b = ', remainder)
44
print('a // b = ', floor_division)
45
print('a ** b = ', exponential)
46

47
# Declaring values and organizing them together
48
num_one = 3
49
num_two = 4
50

51
# Arithmetic operations
52
total = num_one + num_two
53
diff = num_two - num_one
54
product = num_one * num_two
55
div = num_two / num_two
56
remainder = num_two % num_one
57

58
# Printing values with label
59
print('total: ', total)
60
print('difference: ', diff)
61
print('product: ', product)
62
print('division: ', div)
63
print('remainder: ', remainder)
64

65

66
# Calculating area of a circle
67
radius = 10                                 # radius of a circle
68
area_of_circle = 3.14 * radius ** 2         # two * sign means exponent or power
69
print('Area of a circle:', area_of_circle)
70

71
# Calculating area of a rectangle
72
length = 10
73
width = 20
74
area_of_rectangle = length * width
75
print('Area of rectangle:', area_of_rectangle)
76

77
# Calculating a weight of an object
78
mass = 75
79
gravity = 9.81
80
weight = mass * gravity
81
print(weight, 'N')
82

83
print(3 > 2)     # True, because 3 is greater than 2
84
print(3 >= 2)    # True, because 3 is greater than 2
85
print(3 < 2)     # False,  because 3 is greater than 2
86
print(2 < 3)     # True, because 2 is less than 3
87
print(2 <= 3)    # True, because 2 is less than 3
88
print(3 == 2)    # False, because 3 is not equal to 2
89
print(3 != 2)    # True, because 3 is not equal to 2
90
print(len('mango') == len('avocado'))  # False
91
print(len('mango') != len('avocado'))  # True
92
print(len('mango') < len('avocado'))   # True
93
print(len('milk') != len('meat'))      # False
94
print(len('milk') == len('meat'))      # True
95
print(len('tomato') == len('potato'))  # True
96
print(len('python') > len('dragon'))   # False
97

98
# Boolean comparison
99
print('True == True: ', True == True)
100
print('True == False: ', True == False)
101
print('False == False:', False == False)
102
print('True and True: ', True and True)
103
print('True or False:', True or False)
104

105
# Another way comparison 
106
print('1 is 1', 1 is 1)                   # True - because the data values are the same
107
print('1 is not 2', 1 is not 2)           # True - because 1 is not 2
108
print('A in Asabeneh', 'A' in 'Asabeneh') # True - A found in the string
109
print('B in Asabeneh', 'B' in 'Asabeneh') # False -there is no uppercase B
110
print('coding' in 'coding for all') # True - because coding for all has the word coding
111
print('a in an:', 'a' in 'an')      # True
112
print('4 is 2 ** 2:', 4 is 2 ** 2)   # True
113

114
print(3 > 2 and 4 > 3) # True - because both statements are true
115
print(3 > 2 and 4 < 3) # False - because the second statement is false
116
print(3 < 2 and 4 < 3) # False - because both statements are false
117
print(3 > 2 or 4 > 3)  # True - because both statements are true
118
print(3 > 2 or 4 < 3)  # True - because one of the statement is true
119
print(3 < 2 or 4 < 3)  # False - because both statements are false
120
print(not 3 > 2)     # False - because 3 > 2 is true, then not True gives False
121
print(not True)      # False - Negation, the not operator turns true to false
122
print(not False)     # True
123
print(not not True)  # True
124
print(not not False) # False

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.