|
1 | | -#----------------------------------------------------------------------------------------- |
| 1 | +""" |
| 2 | +--------------------------------------------------------------------- |
2 | 3 | # VARIABLES WITH PYTHON |
3 | | -#----------------------------------------------------------------------- |
| 4 | +#-------------------------------------------------------------------- |
| 5 | +
|
4 | 6 | # In python variables are used to store values that can be refrenced and manipulated during program execution |
5 | 7 | # A variable is essentially a name that is assigned to a values |
6 | | - |
| 8 | +""" |
7 | 9 | name = "David" # This is the word David stored inside the variable <name> |
8 | 10 | age = 28 # 28 is stored inside of the variable <age> |
9 | 11 |
|
|
12 | 14 |
|
13 | 15 | # When you run : |
14 | 16 | # python3 .\variables.py |
15 | | - |
16 | 17 | # Output should look like : |
17 | | - |
18 | 18 | """ |
19 | 19 | # David |
20 | 20 | # 28 |
21 | | -
|
22 | 21 | """ |
23 | | - |
24 | | -#--------------------------------------------------------------------------- |
25 | | - |
| 22 | +""" |
| 23 | +------------------------------------------------------------------------ |
26 | 24 | # Rules for naming variables |
27 | 25 | # Variable names can only contain letters, digits and underscores |
28 | 26 | # A variable name cannot start with a digit |
29 | 27 | # Variables are case sensitive |
30 | | - |
31 | | - |
32 | | -#------------------------------------------------------------------ |
33 | 28 | # Variables in Python are assigned values using the = operator. |
34 | | - |
| 29 | +------------------------------------------------------------------------ |
| 30 | +""" |
35 | 31 | x = 5 |
36 | 32 | y = 3.14 |
37 | 33 | z = "HI" # The = sign assigns the value to the variable |
38 | | - |
39 | | - |
40 | | -#------------------------------------------------------------ |
| 34 | +""" |
| 35 | +------------------------------------------------------------ |
41 | 36 | # Dynamic Typing |
42 | | -#-------------------------------------------------------------- |
| 37 | +-------------------------------------------------------------- |
43 | 38 | # Python Variables are dynamically typed |
44 | 39 | # This means the same variable can hold different types of values during execution |
45 | 40 | # Example: |
|
49 | 44 | # If we print it out it will look like |
50 | 45 | print(x) |
51 | 46 | # The output would be: |
52 | | -""" |
53 | | -Ten instead of 10 |
54 | 47 |
|
| 48 | +Ten instead of 10 |
55 | 49 | """ |
56 | 50 |
|
57 | | -# if we want to print out both we need to print the first variable before we assign the new value to the second variable |
| 51 | +""" |
| 52 | +# if we want to print out both we need to print the first variable before we assign the new value to the second variable""" |
58 | 53 | # Example : |
59 | 54 | x = 10 |
60 | 55 | print(x) |
61 | 56 | x = "Ten" |
62 | 57 | print(x) |
63 | 58 |
|
64 | | -# The output would look like : |
65 | | -""" |
| 59 | +"""The output would look like : |
| 60 | +
|
66 | 61 | 10 |
67 | 62 | Ten |
68 | | -
|
69 | 63 | """ |
70 | | - |
71 | | - |
72 | | -#------------------------------------------- |
| 64 | +""" |
| 65 | +------------------------------------------- |
73 | 66 | # Multiple Assignments |
74 | | -#------------------------------------------- |
| 67 | +------------------------------------------- |
75 | 68 | # Python allows multiple variables to be assigned values in a single line |
| 69 | +""" |
76 | 70 |
|
77 | 71 | a = b = c = 100 |
78 | 72 | print(a, b, c) |
79 | | -# Output would look like: |
80 | | -""" |
81 | | -100 100 100 |
82 | 73 |
|
83 | | -""" |
| 74 | +"""# Output would look like: |
84 | 75 |
|
85 | | - |
86 | | -#-------------------------------------------------------- |
| 76 | +100 100 100 |
| 77 | +""" |
| 78 | +""" |
| 79 | +-------------------------------------------------------- |
87 | 80 | # Assigning Different Values |
88 | | -#-------------------------------------------------------- |
| 81 | +-------------------------------------------------------- |
89 | 82 | # We can assigne different values to multiple variables simultaneously |
90 | 83 | # This makes the code concise and easier to read |
| 84 | +""" |
91 | 85 |
|
92 | 86 | x, y, z = 3, 1.4, "Python" # x, = 3 | y, = 1.4 | z = "Python" |
93 | 87 | print(x, y, z) # This will then print out all those values |
94 | | -# The output would look like this: |
95 | | -""" |
96 | | -3 1.4 Python |
97 | | -
|
98 | | -""" |
99 | 88 |
|
| 89 | +"""# The output would look like this: |
100 | 90 |
|
101 | | -#------------------------------------------------------------------ |
| 91 | +3 1.4 Python |
| 92 | +""" |
| 93 | +""" |
| 94 | +------------------------------------------------------------------ |
102 | 95 | # Type Casting a Variable |
103 | | -#------------------------------------------------------------------ |
| 96 | +------------------------------------------------------------------ |
104 | 97 | # Type casting refers to the process of converting the value of- |
105 | 98 | # one data type into another |
106 | 99 |
|
107 | 100 | # Basic Castin Functions |
108 | 101 | # int() |
109 | 102 | # float() |
110 | 103 | # str() |
| 104 | +""" |
111 | 105 | # Example: |
112 | | - |
113 | 106 | str_ing = "10" # This is a string or is initially a string |
114 | 107 | new_string_to_int = int(str_ing) # This converts the string "10" into an integer 10 |
115 | 108 |
|
|
122 | 115 | print(int_to_float) |
123 | 116 | print(age_tostring) |
124 | 117 |
|
125 | | -# The output would look like : |
126 | | -""" |
| 118 | +"""# The output would look like : |
| 119 | +
|
127 | 120 | 10 |
128 | 121 | 5.0 |
129 | 122 | 25 |
130 | | -
|
131 | 123 | """ |
132 | | -#----------------------------------------------------------------------------- |
| 124 | +""" |
| 125 | +-------------------------------------------------------------------------- |
133 | 126 | # Getting the Type of Variable |
134 | | -#----------------------------------------------------------------------------- |
| 127 | +----------------------------------------------------------------------------- |
135 | 128 | # In python we can determine the type of variable using the type() function. |
136 | 129 | # This returns the type of object passed to it |
137 | | - |
138 | | - |
| 130 | +""" |
139 | 131 | a = 5 |
140 | 132 | b = "hello" |
141 | 133 | c = "0.5" |
|
146 | 138 | print(type(c)) |
147 | 139 | print(type(d)) |
148 | 140 |
|
149 | | -# Output will look like |
150 | | -""" |
| 141 | +"""output will look like: |
| 142 | +
|
151 | 143 | <class 'int'> |
152 | 144 | <class 'str'> |
153 | 145 | <class 'str'> |
154 | 146 | <class 'bool'> |
155 | | - |
156 | | - |
157 | 147 | """ |
158 | 148 |
|
159 | | -#---------------------User input using variables----------- |
| 149 | +""" |
| 150 | +---------------------User input using variables----------- |
160 | 151 |
|
161 | 152 | # In Order to get user input we have to assign the input() function. |
162 | | - |
| 153 | +""" |
163 | 154 | nam = input("\nWhat is your name: ") |
164 | 155 | print(nam) |
165 | 156 |
|
166 | 157 | fav_food = input("\nWhat is your favourite food ") |
167 | 158 | print(fav_food) |
168 | | - |
169 | 159 | # You may add your own name and fav food, I just used david and pizza as an example |
170 | 160 |
|
171 | | -# Output would look like: |
172 | | -""" |
| 161 | +"""# Output would look like: |
| 162 | +
|
173 | 163 | What is your name: david |
174 | 164 | david |
175 | 165 |
|
|
178 | 168 |
|
179 | 169 | """ |
180 | 170 |
|
181 | | -#------------------------Input sentences------------------ |
| 171 | +""" |
| 172 | +------------------------Input sentences------------------ |
182 | 173 | # We can also print out sentences like we would with a variable |
183 | 174 | # We use an f string for cleaner code |
184 | | - |
| 175 | +""" |
185 | 176 | nam1 = input("\nWhat is your dogs name?: ") |
186 | 177 | print(f"I can't believe your dogs name is {nam1}") # We can use an f string for cleaner code |
187 | 178 |
|
188 | 179 | nam2 = input("\nWhere do you live?: ") |
189 | 180 | print(f"Oh you live in {nam2} I have no idea where that is") |
190 | 181 |
|
191 | | -# The output would look like this |
192 | | -""" |
| 182 | +"""# The output would look like this |
| 183 | +
|
193 | 184 | What is your dogs name?: Linux |
194 | 185 | I can't believe your dogs name is Linux |
195 | 186 |
|
196 | 187 | Where do you live?: Planet Python |
197 | 188 | Oh you live in Planet Python I have no idea where that is |
198 | 189 |
|
199 | 190 | """ |
200 | | - |
201 | 191 | # We can modify this with if statements, However we will get to that in the conditionals.py file and mini projects folder |
202 | | - |
203 | | - |
204 | | - |
205 | | - |
206 | | - |
207 | | - |
0 commit comments