-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode python-v1.3.py
More file actions
380 lines (350 loc) · 9.42 KB
/
Copy pathcode python-v1.3.py
File metadata and controls
380 lines (350 loc) · 9.42 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# to print words
# print("***",'****','''*****''') => string
# pirnt(****) => integer
# print(**.**) => float
print('Hello "Python"', "Hello 'Python", '''Hello
Python''')
print(22)
print(3.5)
#---------------------------------------------------------------------
# Comments
# To make a comment, we use the hashtag. (#)
#---------------------------------------------------------------------
# Backslash
# To weave the lines together we use (\) Backslash
# To go to a new line we use (\n) Backslash newline
# To put a tab in the line we use (\t) Backslash tab
# To place the backslash itself we use (\\) Backslash Backslash
# To put a single quotation, we use (\') Backslash Single Quotation
# To put a double quotations, we use (\") Backslash Double Quotations
# To repair the writing we use (\r) Backslash Repair
# To make a backspace we use (\b) Backslash Backspace
print("Hello \
Python")
print("Hello\n Python")
print("Hello \t Python")
print("Hello \\ Python")
print("Hello \' Python")
print("Hello \" Python")
print("Hello Python \r")
print(" Hello \b Python")
#---------------------------------------------------------------------
# Data types
# string Here is the writing data And it represents str()
print("Hello Python")
print("Hello Python in 2025")
print(" your age is 22.5")
# integer Here is the integer data And it represents int()
print(22)
# float This is the float data and it represents something float()
print(3.5)
# Boolean values are logical values in Python and represent bool()
#False
print(22 == 33)
#True
print(22 == 22)
# The empty value represents none
variable = None
# Lists are ordered structures and represent list()
# variable = [*, *, *, *]
print([1,2,3])
# Dictionaries are data that have keys and represent dict()
# variable = {"key" : "value", "key" : "value"}
# Variable.keys()
# Variable.values()
# Variable.items()
variable = {"name" : "ahmed", "age" : 21}
# Sets are unordered sets and represent set()
# variable = {*, *, *, *}
variable = {1, 2, 3, 4}
# sets are immutable
variable = frozenset([1, 2, 3, 4])
# Rows are immutable lists
#variable = (*, *, *, *)
variable = (1, 2, 3, 4)
#---------------------------------------------------------------------
# Arithmetic operations
# For plural we use (+)
# To subtract we use (-)
# To multiply we use (*)
# To divide we use (/)
# For approximate division we use (//)
# For the rest of the division we use (%)
# For exponent we use (**)
print(2 + 3)
print(3 - 2)
print(3 * 2)
print(3 / 10)
print(3 // 10)
print(3 % 10)
print(3 ** 3)
#---------------------------------------------------------------------
# To know the type of data we use type()
type("Hello Python")
#---------------------------------------------------------------------
# To enter information into the program we use input()
# if it was string
input("Hello Python")
# if it was integer
int(input(3))
# if it was float
float(input(3.5))
#---------------------------------------------------------------------
# To create an exception in Python we use try: and except:
# try:
# syntax
# except:
# syntax
try:
int(input("Enter first number: "))
int(input("Enter last number: "))
except:
print("error")
#---------------------------------------------------------------------
# variables
# To set the value of the base variable, say:
# variable = value
# The variable can be string, integer, float, or any data type.
x = "Hello Python" # string
x = 3 # integer
x = 3.5 # float
# Arithmetic operations can be performed within the variable.
x = 3 + 4
x = 3 + 3.5
# or
x = 3
z = 4
n = 3.5
y = x + z
w = x - n
#The variable value can be modified.
x = 3
x += 2
# The value of x is currently 5.
x = 3
x -= 2
# The value of x is currently 1.
x = 3
x *= 2
# The value of x is currently 6.
x = 10
x /= 2
# The value of x is currently 5.
x = 10
x %= 3
# The value of x is currently 1.
x = 5
x **= 2
# The value of x is currently 25.
# This is not only for integer but also for string and float.
# If you want to remove an item from the variables list you can use del
del x
# To know the address of a variable in temporary memory, we use id()
x = 1
print(id(x))
#---------------------------------------------------------------------
# if loop
# To use conditional statements in Python, we use the if command.
#---------------------------------------------------------------------
# if variable == value :
# syntax
# elif variable == value :
# syntax
# else:
# syntax
#---------------------------------------------------------------------
if x == 10 :
print("yes")
elif x == 5 :
print("no")
else:
print("error")
# Warning: You can use more than one elif in an if statement, and you can use more than one statement in a syntax.
#---------------------------------------------------------------------
# for loop
# To create a loop in Python we use for
#---------------------------------------------------------------------
# for variable in value or syntax :
# print(variable or value)
#---------------------------------------------------------------------
x = "Hello Python"
for z in x :
print(z)
# or
for z in range(3) :
print(z)
#---------------------------------------------------------------------
# while loop
# To make a continuous loop as long as the condition is met, we use while
#---------------------------------------------------------------------
# while variable or value == variable or value :
# syntax
# break
# continue
#---------------------------------------------------------------------
x = 2
while x < 10 :
print("Hello")
x += 1
# To stop the process we used break
x = 2
while x < 10 :
print("Hello")
break
#---------------------------------------------------------------------
# break and continue
# To stop the process, but limits we use:
for x in range(10) :
if x == 5 :
break
print(x)
# to continue the process we used continue
for x in range(10) :
print(x)
if x == 5 :
print("You have reached 5")
Question = input("Do you want to continue or leave(continue=y)(leave=n): ")
if Question == "y" :
continue
elif Question == "n" :
break
else :
print("error")
break
#---------------------------------------------------------------------
# functions
# To create a function that can be called at any time, we use def
# def variable():
# syntax or variable
def welcome():
print("Hello Python")
welcome()
# or
def welcome():
pass
welcome()
# or
def employee(name,job):
print(name)
print(job)
employee('ahmed', 'pro')
# or
def home(Kitchen,bedroom):
print(f"this is {Kitchen}")
print(f"this is {bedroom}")
home(Kitchen=50, bedroom=30)
#---------------------------------------------------------------------
# To import in Python we use import
# import class from another file or library
import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
#or
import python2
python2.color()
python2.number()
#---------------------------------------------------------------------
# class
# To create Object-Oriented Programming - OOP we use class
# class Variable:
# def variable():
# syntax or variable
# variable = Variable on class
# variable.syntax
class Home:
print("Hello Python")
y = "Hi"
def home(self):
print(f"Kitchen")
print(f"bedroom")
x = Home()
print(x.y)
x.home()
# or
class Home:
def __init__(self):
print("Hello Home")
#---------------------------------------------------------------------
# Handling files and folder in Python
# w => write
# r => read
# a => append
# Create files (w)
x = open("Hello.txt", "w")
x.write("Hello Python")
x.close()
# Writing to files
x = open("Hello.txt", "a")
x.write("Hello")
x.close()
# Read files
x = open("Hello.txt", "r")
y = x.read()
print(y)
# Check file
import os
if os.path.exists("python.py"):
print("yes")
else:
print("no")
# remove file
import os
os.remove("Hello.txt")
# create folder
import os
os.mkdir("Hello")
# Check the folder
import os
if os.path.exists("Hello"):
print("yes")
else:
print("no")
# remove folder
import os
os.rmdir("Hello")
# listdir in folder
import os
x = os.listdir("Hello")
print(x)
# or
import os
x = os.listdir("hello")
for file in x:
print(file)
#---------------------------------------------------------------------
# framework random
# To get a random result from 0 to 1 we use random.random
import random
x = random.random()
print(x)
# or
import random
print(random.random())
# To choose a random numeric value we use random.randint(*, *)
import random
x = random.randint(1, 10)
print(x)
# or
import random
print(random.randint(1, 10))
# To randomly select an alternative from the alternatives, we use random.choice()
import random
x = ["hi", "hello", "Hello", 1, 2]
z = random.choice(x)
print(z)
# or
import random
x = ["hi", "hello", "Hello", 1, 2]
print(random.choice(x))
# To mix values we use random.shuffle()
import random
x = ["hi", "hello", "Hello", 1, 2]
z = random.shuffle(x)
print(z)
# Select some value or values from the basic values we use random.sample()
import random
x = ["hi", "hello", "Hello", 1, 2]
z = random.sample(x , 3)
print(z)