You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 02_Day_Variables_builtin_functions/02_variables_builtin_functions.md
+31-27Lines changed: 31 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@
33
33
34
34
## Built in functions
35
35
36
-
In Python we have lots of built-in functions. Built-in functions are globally available for your use that mean you can make use of the built-in functions without importing or configuring. Some of the most commonly used Python built-in functions are the following: _print()_, _len()_, _type()_, _int()_, _float()_, _str()_, _input()_, _list()_, _dict()_, _min()_, _max()_, _sum()_, _sorted()_, _open()_, _file()_, _help()_, and _dir()_. In the following table you will see an exhaustive list of Python built-in functions taken from [python documentation](https://docs.python.org/3.9/library/functions.html).
36
+
In Python we have lots of built-in functions. Built-in functions are globally available for your use that mean you can make use of the built-in functions without importing or configuring. Some of the most commonly used Python built-in functions are the following: _print()_, _len()_, _type()_, _int()_, _float()_, _str()_, _input()_, _list()_, _dict()_, _min()_, _max()_, _sum()_, _sorted()_, _open()_, _file()_, _help()_, and _dir()_. In the following table you will see an exhaustive list of Python built-in functions taken from [python documentation](https://docs.python.org/3/library/functions.html).
- Casting: Converting one data type to another data type. We use _int()_, _float()_, _str()_, _list_, _set_
209
209
When we do arithmetic operations string numbers should be first converted to int or float otherwise it will return an error. If we concatenate a number with a string, the number should be first converted to a string. We will talk about concatenation in String section.
210
210
211
-
**Example:**
211
+
**Examples:**
212
212
213
213
```py
214
214
# int to float
@@ -229,8 +229,12 @@ print(num_str) # '10'
229
229
230
230
# str to int or float
231
231
num_str ='10.6'
232
+
num_float =float(num_str) # Convert the string to a float first
233
+
num_int =int(num_float) # Then convert the float to an integer
232
234
print('num_int', int(num_str)) # 10
233
235
print('num_float', float(num_str)) # 10.6
236
+
num_int =int(num_float)
237
+
print('num_int', int(num_int)) # 10
234
238
235
239
# str to list
236
240
first_name ='Asabeneh'
@@ -278,22 +282,22 @@ Number data types in Python:
278
282
### Exercises: Level 2
279
283
280
284
1. Check the data type of all your variables using type() built-in function
281
-
1. Using the _len()_ built-in function, find the length of your first name
282
-
1. Compare the length of your first name and your last name
283
-
1. Declare 5 as num_one and 4 as num_two
284
-
1. Add num_one and num_two and assign the value to a variable total
285
-
2. Subtract num_two from num_one and assign the value to a variable diff
286
-
3. Multiply num_two and num_one and assign the value to a variable product
287
-
4. Divide num_one by num_two and assign the value to a variable division
288
-
5. Use modulus division to find num_two divided by num_one and assign the value to a variable remainder
289
-
6. Calculate num_one to the power of num_two and assign the value to a variable exp
290
-
7. Find floor division of num_one by num_two and assign the value to a variable floor_division
291
-
1. The radius of a circle is 30 meters.
285
+
2. Using the _len()_ built-in function, find the length of your first name
286
+
3. Compare the length of your first name and your last name
287
+
4. Declare 5 as num_one and 4 as num_two
288
+
5. Add num_one and num_two and assign the value to a variable total
289
+
6. Subtract num_two from num_one and assign the value to a variable diff
290
+
7. Multiply num_two and num_one and assign the value to a variable product
291
+
8. Divide num_one by num_two and assign the value to a variable division
292
+
9. Use modulus division to find num_two divided by num_one and assign the value to a variable remainder
293
+
10. Calculate num_one to the power of num_two and assign the value to a variable exp
294
+
11. Find floor division of num_one by num_two and assign the value to a variable floor_division
295
+
12. The radius of a circle is 30 meters.
292
296
1. Calculate the area of a circle and assign the value to a variable name of _area_of_circle_
293
297
2. Calculate the circumference of a circle and assign the value to a variable name of _circum_of_circle_
294
298
3. Take radius as user input and calculate the area.
295
-
1. Use the built-in input function to get first name, last name, country and age from a user and store the value to their corresponding variable names
296
-
1. Run help('keywords') in Python shell or in your file to check for the Python reserved words or keywords
299
+
13. Use the built-in input function to get first name, last name, country and age from a user and store the value to their corresponding variable names
300
+
14. Run help('keywords') in Python shell or in your file to check for the Python reserved words or keywords
print('Division without the remainder: ', 7//2) # 3, gives without the floating number or without the remaining
76
76
print ('Division without the remainder: ',7//3) # 2
77
77
print('Modulus: ', 3%2) # 1, Gives the remainder
78
-
print('Exponentiation: ', 2**3) #9 it means 2 * 2 * 2
78
+
print('Exponentiation: ', 2**3) #8 it means 2 * 2 * 2
79
79
```
80
80
81
81
**Example:Floats**
@@ -121,7 +121,7 @@ print('a * b = ', product)
121
121
print('a / b = ', division)
122
122
print('a % b = ', remainder)
123
123
print('a // b = ', floor_division)
124
-
print('a ** b = ', exponentiation)
124
+
print('a ** b = ', exponential)
125
125
```
126
126
127
127
**Example:**
@@ -148,7 +148,7 @@ print('division: ', div)
148
148
print('remainder: ', remainder)
149
149
```
150
150
151
-
Let us start start connecting the dots and start making use of what we already know to calculate (area, volume,density, weight, perimeter, distance, force).
151
+
Let us start connecting the dots and start making use of what we already know to calculate (area, volume,density, weight, perimeter, distance, force).
152
152
153
153
**Example:**
154
154
@@ -174,6 +174,7 @@ print(weight, 'N') # Adding unit to the weight
174
174
mass =75# in Kg
175
175
volume =0.075# in cubic meter
176
176
density = mass / volume # 1000 Kg/m^3
177
+
print(density, 'Kg/m^3') # Adding unit to the density
177
178
178
179
```
179
180
@@ -213,13 +214,13 @@ In addition to the above comparison operator Python uses:
213
214
-_is_: Returns true if both variables are the same object(x is y)
214
215
-_is not_: Returns true if both variables are not the same object(x is not y)
215
216
-_in_: Returns True if the queried list contains a certain item(x in y)
216
-
-_not in_: Returns True if the queried list doesn't have a certain item(x in y)
217
+
-_not in_: Returns True if the queried list doesn't have a certain item(x not in y)
217
218
218
219
```py
219
220
print('1 is 1', 1is1) # True - because the data values are the same
220
221
print('1 is not 2', 1isnot2) # True - because 1 is not 2
221
222
print('A in Asabeneh', 'A'in'Asabeneh') # True - A found in the string
222
-
print('B in Asabeneh', 'B'in'Asabeneh') # False - there is no uppercase B
223
+
print('B not in Asabeneh', 'B'in'Asabeneh') # False - there is no uppercase B
223
224
print('coding'in'coding for all') # True - because coding for all has the word coding
224
225
print('a in an:', 'a'in'an') # True
225
226
print('4 is 2 ** 2:', 4is2**2) # True
@@ -287,7 +288,7 @@ The perimeter of the triangle is 12
287
288
18. Check if the floor division of 7 by 3 is equal to the int converted value of 2.7.
288
289
19. Check if type of '10' is equal to type of 10
289
290
20. Check if int('9.8') is equal to 10
290
-
21.Writ a script that prompts the user to enter hours and rate per hour. Calculate pay of the person?
291
+
21.Write a script that prompts the user to enter hours and rate per hour. Calculate pay of the person?
25. Slice out the phrase 'because because because'in the following sentence: 'You cannot end a sentence with because because because is a conjunction'
561
562
26. Find the position of the first occurrence of the word 'because'in the following sentence: 'You cannot end a sentence with because because because is a conjunction'
562
563
27. Slice out the phrase 'because because because'in the following sentence: 'You cannot end a sentence with because because because is a conjunction'
563
-
28. Does '\'Coding For All' start with a substring _Coding_?
564
+
28. Does 'Coding For All' start with a substring _Coding_?
564
565
29. Does 'Coding For All' end with a substring _coding_?
565
566
30. ' Coding For All ' , remove the left and right trailing spaces in the given string.
566
567
31. Which one of the following variables returnTrue when we use the method isidentifier():
0 commit comments