-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.py
130 lines (110 loc) · 3.35 KB
/
expression.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
# -*- coding: utf-8 -*-
"""
Created on Tue May 17 12:57:20 2022
@author: Allen
"""
#VARIABLES
#Python has no command for declaring a var.
#Variable will be automatically created the moment you 1st assign a value into it.
a = 2
b = "lielly"
print(a)
print(b)
#CASTING
#If you want to specify/convert the type of the var, it can be done with Casting.
a = str(1) #--> a = '1'
b = int(3) #--> b = 3
print(a)
print(b)
#nb:
#To get the data type, you can use 'type()', i.e type(a) --> <class 'str'>.
#For string type, you can use either '' or "", both are valid to use.
#MULTIPLE VALUES VARIABLES
#You can assign multiple value for multiple vars.
#nb. Python does not have an inbuilt double data type,
#but it has a float type that designates a floating-point number.
a, b, c = 1, 'john', float(3.5)
print(a)
print(b)
print(c)
a = b = c = 'same'
print(a)
print(b)
print(c)
#COLLECTION & OUTPUT
#nb:
#List [x, y, z]: Mutable, duplicatable, heterogeneous, ordered
#Tuple (x, y, z): Immutable, duplicatable, heterogeneous, ordered
#Set {x, y, z}: Mutable, unordered, unduplicatable, no indices
#Frozenset {x,y,z}: Immutable, unordered, unduplicatable, no indices
#Dict {map(k, v)}: key is unduplicatable
animal = ['butterfly', 'dragon', 'fish']
a = animal[0]
b = animal[1]
c = animal[2]
print(a, b, c)
#d = animal[3]
#print(d) #index out of range
x = y = animal
print(x, y) #--> ['butterfly', 'dragon', 'fish']
#GLOBAL VAR
x = 'this is a global variable'
def yourFunc():
global x
x = 'reassign previous x'
print('x : ' + x)
yourFunc()
print('x after reassigned by global keyword : ' + x)
#DATA TYPES
dStr = 'kelp' #TEXT: str
dNum = 22 #NUMERIC: int, float, complex
dFloat = 22e3 #e = 10^
dComplex = 1j
dList = [1,2,3,3] #SEQUENCE: list (mutable), tuple(immutable) {duplicatable}; range
dTuple = (1,2,3,3)
dRange = range(6) # 0 - 6
dDict = {'name': 'john', 'age': 36} #MAP: dict(ionary)
dSet = {'1', '2', '3'} #SET: set (mutable), frozenset (immutable) {unduplicatable}.
dFrozenset = frozenset({'1','2','3'})
dBool = False #BOOL: bool
dBytesInt = bytes(4) #BINARY: bytes, bytearray, memoryview
dBytesStr = b'hello'
dByteArray = bytearray(4)
dMemoryView = memoryview(bytes(5)) #print the memory allocation from given obj.
dNone = None #NONE: none == null
#STRING
#https://www.w3schools.com/python/python_strings.asp
#STRING, MULTILINE STRINGS
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)
print(a[1])
#LOOPING
text = 'Lin Qibao'
for char in text:
print(char) #--> l, i, n, , q, i, ...
#TUPPLE
#Since tupple is immutable, you need to convert it to list type to make it mutable.
#https://www.w3schools.com/python/python_tuples_update.asp
#DICTIONARY
#.keys() & .items()
#https://www.w3schools.com/python/python_dictionaries_loop.asp
print()
cars = {
"brand":"toyota",
"name":"CRV",
"year":"2022",
"engine":{
"name":"diesel",
"year":"2021"
}
}
print(cars.get("name"))
print(cars.get("engine").get("year"))
cars["color"] = "dope"
cars["engine"].setdefault("brand", "tom")
print(cars)
for attr in cars:
print(cars[attr])