-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathType-VARIABLES.py
More file actions
114 lines (87 loc) · 2.31 KB
/
Type-VARIABLES.py
File metadata and controls
114 lines (87 loc) · 2.31 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
########## VARIABLES ##########
## RULES
# The PEP 8 -- Style Guide for Python Code recommends the following naming convention for variables and functions in Python:
# variable names should be lowercase, with words separated by underscores to improve readability (e.g., var, my_variable)
# function names follow the same convention as variable names (e.g., fun, my_function)
# it's also possible to use mixed case (e.g., myVariable), but only in contexts where that's already the prevailing style, to retain backward compatibility with the adopted convention
# a variable is name + value, the value is a string or a number or a Boolean
# name = uper/lower-case,digits,_, and punctuation
# CANNOT START BY A NUMBER
# MUST START BY A LETTER (_ is a letter)
# no space
# only latin characters
# forbidden special characters :"',<>/?|\()!@#$%^&*~-+
# case sensitive
# CANNOT BE A RESERVED PYTHON KEYWORD
['False', 'None', 'True', 'and', 'as', 'assert', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
'finally', 'for', 'from', 'global', 'if', 'import', 'in',
'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise',
'return', 'try', 'while', 'with', 'yield']
# best pratcice: lower case, avoid including special python terms
## simple var
my_dogs=2
print (my_dogs)
# 2
## printing var and strings
var = "3.8.5"
print("Python version: " + var)
# Python version: 3.8.5
## printing var and numbers
a = 3.0
b = 4.0
c = (a ** 2 + b ** 2) ** 0.5
print("c =", c)
# c = 5.0
var1 = "ab"
print(2*var1)
# abab
## VARIABLE CAN CHANGE THEIR DATA TYPE DURING CODE EXECUTION
my_dogs=2 # = INT
print (my_dogs)
# 2
# ... later on
my_dogs=["kad","imane"] # = LIST
print (my_dogs)
# ['kad', 'imane']
## operations with variables
# INT
j=3
m=5
a=6
total_apples=j+m+a
print("John has",j,"apples,","Mary ",m,"and Adam",a)
print("Total number of apples:",total_apples)
# John has 3 apples, Mary 5 and Adam 6
# Total number of apples: 14
# INT + FLOAT
a = 1
b = 2.
c = a + b
print(c)
# 3.0
# INT + BOOL
a = 1
b = True # True = 1
c = a + b
print(c)
# 2
a = 1
b = False # False = 0
c = a + b
print(c)
# 1
## shortcup operator
x=x*2 = x*=2
b=b+10 = b+=10
var = 1
print(var)
# 1
var = var + 1
print(var)
# 2
var+=1
print(var)
# 3
print(var+=1) # you MUST redefine variable before printing it
# SyntaxError: invalid syntax