-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariableT.py
More file actions
137 lines (124 loc) · 3.97 KB
/
Copy pathvariableT.py
File metadata and controls
137 lines (124 loc) · 3.97 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
# CREATING VARIABLE
x = 10.583753
name = "Python"
a = b = c = 5 # multiple assignment
p, q = 1, 2 # unpacking during assignment
#input
x = float(input("Enter a number: "))
name = input("Enter your name: ")
# printing and formatting
print("Id is",end=" ") # end parameter to avoid newline
print(id(name)) # memory address of the variable
print(type(x)) # <class 'int'>
print(type(name)) # <class 'str'>
print(name)
print("Value:", x)
print(name, x, sep=" | ") # Name: Python | Value: 10.583753
print(f"Name: {name}, Value: {x}") # f-string formatting
print("Name: {}, Value: {}".format(name, x)) # str.format() method
print(f"Value: {x:3.2f}") # 2 decimal places and 3 digit place reserved before decimal
print("Age: {1} Name: {0}".format(name, x)) # with index
print("Name: {n} Age: {a}".format(n=name, a=x)) # with keyword
# alignment
print(f"{name:>10}") # right align
print(f"{name:<10}") # left align
print(f"{name:^10}") # center align
# DATA TYPES
# numeric
i = 10 # int
f = 3.14 # float
c = 2 + 3j # complex
# string
s = "hello"
# sequence
lst = [1, 2, 3] # list
tup = (1, 2, 3) # tuple
rng = range(5) # range
# binary
b = bytes([65, 66]) # byte
ba = bytearray([65, 66]) # bytearray
mv = memoryview(b) # memoryview
# dictionary
dct = {"a": 1, "b": 2}
# set
st = {1, 2, 3}
fs = frozenset([1, 2, 3])
# boolean
flag = True # boolean value can be True or False
#0 and 1 can also be used as boolean values where 0 is False and 1 is True
flag=bool(0) # False
flag=bool(1) # True
flag=bool(None) # False
flag=bool("") # False
flag=bool("False") # True (non-empty string is True)
flag=bool([]) # False
flag=bool([0]) # True (non-empty list is True)
flag=bool({}) # False
flag=bool({0: "zero"}) # True (non-empty dict is True)
flag=bool(set()) # False
flag=bool({0}) # True (non-empty set is True)
flag=bool(0.0) # False
flag=bool(0.1) # True (non-zero float is True)
if(1):print("This will always execute as 1 is treated as True")
if(not None): print("This will execute as None is treated as False")
# none
n = None
#type of variable:global, local, nonlocal
global_var1 = "global"
global_var2 = "global"
def outer():
outer1 = "local"
outer2 = "local"
global_var2 = "local" #creating new variable, not representing global variable
global global_var1 #represent global variable, not creating new variable
global_var1 = "modified global"
def inner():
nonlocal outer1 #represent immediate outer variable, it is not creating new variable
outer1 = "nonlocal"
outer2 = "local" #creating new variable, not representing outer variable
global global_var1 #represent global variable, not creating new variable
global_var1 = "modified global from inner"
print("inner:", outer1)
print("inner:", outer2)
print("inner:", global_var1)
inner()
print("outer:", outer1)
print("outer:", global_var1)
outer()
# TYPE CASTING
# implicit
x = 10
y = 2.5
z = x + y # int -> float automatically
print(z)
# explicit
print(int("10")) # string to int
print(float(5)) # int to float
print(complex(2, 3)) # complex
print(str(100)) # to string
print(tuple([1, 2])) # list -> tuple
print(list((1, 2))) # tuple -> list
print(set([1, 2, 2])) # remove duplicates
print(dict([(1, 'a'), (2, 'b')])) # list of tuples to dict
print(chr(65)) # A
print(ord('A')) # 65
print(hex(255)) # 0xff
print(oct(8)) # 0o10
# eval (use carefully)
print(eval("2 + 3")) # 5
# PACKING AND UNPACKING
# packing
t = 1, 2, 3 # tuple packing
# unpacking
a, b, c = t
print(a, b, c)
# extended unpacking (* for remaining elements)
t = (10, 20, 30, 40, 50, 60)
x, *y, z = t
print(x) # first value
print(y) # middle values
print(z) # last value
# DELETING VARIABLE
temp = 100
del temp
# print(temp) # ERROR: variable deleted