forked from ahmedshaaban1/Python_course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_loop.py
106 lines (90 loc) · 2.14 KB
/
4_loop.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author : Ahmed Shaaban
Date : Sep 5, 2021
purpose : breif on loops.
"""
#%% range (start,end, step) datatype
# what is range? datatype that return object.
print(type(range(4)))
# note the end
print(list(range(0,4)))
# note the begining.
print(list(range(4)))
# note the dataytype
print(type(list(range(4)))) # now list and not range
#%% print numbers from 1 to 10
for i in range(1,10+1):
print(i)
#%% print numbers from 1 to 10 in one line
for i in range(1,10):
print(i,end=" ")
#%% print odd numbers
for i in range(1,10+1,3):
print(i)
#%% print even numbers
for i in range(0,10+1,2):
print(i)
#%% print number from -10,1
for i in range(10,0,-1):
print(i)
#%% reversed function
for i in reversed(range(1,10+1)):
print(i)
#%% print 1 to 10 and their squares.
for i in range(0,10+1):
print(i,i**2)
#%% print range attributes
print(range(3).start)
print(range(3).stop)
print(range(3).step)
#%% classical assingment (sum numbers from 1 to n)
n=10
mysum=0
for i in range(1,n+1):
mysum=mysum+i
print(mysum)
#%% arange (numpy)
import numpy as np
print(type(np.arange(4)))
# no need for list
print(np.arange(4))
#%% another trick to include 10
for i in np.arange(1.,10.1):
print(i)
#%% difference between the arange and range.
np.arange(0.1,1,0.1)
range(0.1,1,0.1)
#%%
import sys
x = range(1,10000)
print(sys.getsizeof(x)) # --> Output is 48
y=list(x)
print(sys.getsizeof(y))
a = np.arange(1,10000)
print(sys.getsizeof(a)) # --> OutPut is 80096
#%%
print(np.arange(-5,1, dtype=np.int32)) # signed int
print(np.arange(-5,1, dtype=np.uint32)) # unsigned int
print(np.arange(-5,1, dtype=np.float16)) #float
#%% accessing list
languages = ["C","C++","Python","Fortran"]
for lang in languages:
print(lang)
#%% another way to access list
for i in range(len(languages)):
print(languages[i])
#%% else after loop
for i in range(5):
print(i)
if (i == 5):
print('oh no')
break
else: # executed if break was not encountred.
print("intersting!")
#%% continue
for i in range(5):
if (i == 2): # skip 2 and continue.
continue
print(i)