/
dummy_python.py
167 lines (109 loc) · 3.39 KB
/
dummy_python.py
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
"""Python is a popular programming language. It was created in 1991 by Guido van Rossum.
You will love it!!!!"""
# Hello World!
print ("Hello World!")
# This is a comment
"""This
is also a comment"""
# Variables
name = 'Khadija' # string
age = 18 # number
# String
a = 'Hi' # a string
b = "Hello" # also a string
# Number Types
c = 120 # integer
d = 1.23 # float
e = 2m # complex
# Input
name = input("What is your name? ") # prints 'What is your name' to the console and takes the input as the new value of 'name'
print("Hello," + name) # returns input
# Operators
# Arithmetic
f = 5 + 5 # 10
g = 5 - 2 # 3
h = 4 * 4 # 16
i = 10 / 2 # 5
j = 6 % 2 # 0 , % means Remainder
# Assignment
k += 4 # k = k + 4
l -= 2 # l = l - 2
m *= 11 # m = m * 11
n /= 9 # n = n / 9
o %= 3 # o = o % 3
# Comparison
# ==
#/ !=
# >
# <
# >=
# <=
p = 8
q = 7
print(x == y) # returns 'False' because 8 is not equal to 7
print(x != y) # returns 'True' because 8 is not equal to 7
print(x > y) # returns 'True' because 8 is greater than 7
print(x < y) # returns 'False' because 8 is greater than 7
print(x >= y) # returns 'True' because 8 is greater than equal to 7
print(x <= y) # returns 'False' because 8 is greater than equal to 7
# Collections
# List is a collection which is ordered and changeable and allows duplicate members.
# Tuple is a collection which is ordered and unchangeable and allows duplicate members.
# Set is a collection which is unordered and unindexed and does not allow duplicate members.
# Dictionary is a collection which is unordered, changeable and indexed and does not allow duplicate members.
r = ["Muhammad", 2, 20, "Sadiq"] # list
s = list(("Muhammad", 2, 20, "Sadiq")) # also list
t = ("Abdul", "Mike") # tuple
u = tuple(("Abdul", "Mike")) # also tuple
v = {"Ali", "Umar", "Yahya"} # set
w = set(("Ali", "Umar", "Yahya")) # also set
x = {
"House": "Mansion",
"Car": "Tesla",
"Phone": "Samsung"
} # dictionary
y = dict(
"House": "Mansion",
"Car": "Tesla",
"Phone": "Samsung"
) # also dictionary
# Conditions
# if..elif..else
# and..or..is..not
z = 99
aa = 10
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
"""if it is raining, take an umbrella
or maybe a rain coat
if not, then just a tshirt would do"""
# Loop
# For Loops
# While loops
ab = ["dev", "design", "google"]
ac = 3
for i in ab: # for loop
print(i)
while i < 3: # while loop
print(i)
i = i + 1
# Functions
def whatever(a,b)
return a + b
whatever(5,5) # returns 10
# Lambda
ad = lambda a, b : a - b
print(ad(5, 6)) # returns -1
# Classes
class Person:
def __init__(whatever, name, age):
whatever.name = name
whatever.age = age
ae = Person("Khadija", 18)
print(ae.name + " " + ae.age)
# that was cool right?
# go practice, the sky is not the limit when it comes to learning, just keep doing it