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
<small> First Edition: Nov 22 - Dec 22, 2019</small>
12
+
<small> Second Edition: July, 2021</small>
13
13
</sub>
14
14
15
-
</div>
16
15
</div>
17
16
18
17
[<< Day 1](../readme.md) | [Day 3 >>](../03_Day_Operators/03_operators.md)
@@ -22,6 +21,7 @@
22
21
-[📘 Day 2](#-day-2)
23
22
-[Built in functions](#built-in-functions)
24
23
-[Variables](#variables)
24
+
-[Declaring Multiple Variable in a Line](#declaring-multiple-variable-in-a-line)
25
25
-[Data Types](#data-types)
26
26
-[Checking Data types and Casting](#checking-data-types-and-casting)
27
27
-[Numbers](#numbers)
@@ -33,36 +33,37 @@
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. 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/2/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).
Let's practice more by using different built-in functions
44
+
Let us practice more by using different built-in functions
45
45
46
46

47
47
48
-
As you can see from the terminal above, python has got reserved words. We do not use reserved words to declare variables or functions. We will cover variables in the next section.
48
+
As you can see from the terminal above, Python has got reserved words. We do not use reserved words to declare variables or functions. We will cover variables in the next section.
49
49
50
-
I believe, by now you are familiar with built-in functions. Let's do one more practice of built-in functions and we will move on to the next section
50
+
I believe, by now you are familiar with built-in functions. Let us do one more practice of built-in functions and we will move on to the next section.
51
51
52
52

53
53
54
54
## Variables
55
55
56
-
Variables store data in a computer memory. Mnemonic variables are recommended to use in many programming languages. A variable refers to a memory address in which data is stored.
57
-
Number at the beginning, special character, hyphen are not allowed when naming them. A variable can have a short name (like x,y,z), but a more descriptive name (firstname, lastname, age, country) is highly recommended.
56
+
Variables store data in a computer memory. Mnemonic variables are recommended to use in many programming languages. A mnemonic variable is a variable name that can be easily remembered and associated. A variable refers to a memory address in which data is stored.
57
+
Number at the beginning, special character, hyphen are not allowed when naming a variable. A variable can have a short name (like x, y, z), but a more descriptive name (firstname, lastname, age, country) is highly recommended.
58
+
58
59
Python Variable Name Rules
59
60
60
61
- A variable name must start with a letter or the underscore character
61
62
- A variable name cannot start with a number
62
63
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and \_ )
63
-
- Variable names are case-sensitive (firstname, Firstname, FirstName and FIRSTNAME are different variables)
64
+
- Variable names are case-sensitive (firstname, Firstname, FirstName and FIRSTNAME) are different variables)
64
65
65
-
Valid variable names
66
+
Here are some example of valid variable names:
66
67
67
68
```shell
68
69
firstname
@@ -74,9 +75,10 @@ first_name
74
75
last_name
75
76
capital_city
76
77
_if # if we want to use reserved word as a variable
77
-
year_2019
78
-
year2019
79
-
current_year_2019
78
+
year_2021
79
+
year2021
80
+
current_year_2021
81
+
birth_year
80
82
num1
81
83
num2
82
84
```
@@ -85,19 +87,20 @@ Invalid variables names
85
87
86
88
```shell
87
89
first-name
90
+
first@name
91
+
first$name
88
92
num-1
89
93
1num
90
94
```
91
95
92
-
We will use standard python variable naming style which has been adopted by many python developers. The example below is an example of standard naming of variables, underscore when the variable name is long.
96
+
We will use standard Python variable naming style which has been adopted by many Python developers. Python developers use snake case(snake_case) variable naming convention. We use underscore character after each word for a variable containing more than one word(eg. first_name, last_name, engine_rotation_speed). The example below is an example of standard naming of variables, underscore is required when the variable name is more than one word.
93
97
94
-
When we assign a certain data type to a variable, it is called variable declaration. For instance in the example below my first name is assigned to a variable first_name. The equal sign is an assignment operator. Assigning means storing data in the variable.
98
+
When we assign a certain data type to a variable, it is called variable declaration. For instance in the example below my first name is assigned to a variable first_name. The equal sign is an assignment operator. Assigning means storing data in the variable. The equal sign in Python is not equality as in Mathematics.
95
99
96
100
_Example:_
97
101
98
102
```py
99
103
# Variables in Python
100
-
101
104
first_name ='Asabeneh'
102
105
last_name ='Yetayeh'
103
106
country ='Finland'
@@ -113,17 +116,17 @@ person_info = {
113
116
}
114
117
```
115
118
116
-
Let's use _print()_ and _len()_ builtin functions. Print function will take multiple arguments. An argument is a value which we pass or put inside the function parenthesis, see the example below.
119
+
Let us use the _print()_ and _len()_ built-in functions. Print function takes unlimited number of arguments. An argument is a value which we can be passed or put inside the function parenthesis, see the example below.
117
120
118
121
**Example:**
119
122
120
123
```py
121
-
print('Hello, World!')
122
-
print('Hello',',', 'World','!') # it can take multiple arguments
124
+
print('Hello, World!')# The text Hello, World! is an argument
125
+
print('Hello',',', 'World','!') # it can take multiple arguments, four arguments have been passed
123
126
print(len('Hello, World!')) # it takes only one argument
124
127
```
125
128
126
-
Let's print and also find the length of the variables declared at the top:
129
+
Let us print and also find the length of the variables declared at the top:
127
130
128
131
**Example:**
129
132
@@ -142,7 +145,9 @@ print('Skills: ', skills)
142
145
print('Person information: ', person_info)
143
146
```
144
147
145
-
Variables can also be declared in one line:
148
+
### Declaring Multiple Variable in a Line
149
+
150
+
Multiple variables can also be declared in one line:
146
151
147
152
**Example:**
148
153
@@ -157,7 +162,7 @@ print('Age: ', age)
157
162
print('Married: ', is_married)
158
163
```
159
164
160
-
Getting user input using the _input()_ built-in function. Let's assign the data we get from a user into first_name and age variables.
165
+
Getting user input using the _input()_ built-in function. Let us assign the data we get from a user into first_name and age variables.
161
166
**Example:**
162
167
163
168
```py
@@ -170,12 +175,12 @@ print(age)
170
175
171
176
## Data Types
172
177
173
-
There are several data types in python. To identify the data type we use the _type_builtin function. I would like you to focus on understanding different data types very well. When it comes to programming, it is all about data types. I introduced data types at the very beginning and it comes again, because every topic is related to data types. We will cover data types in more detail in their respective sections.
178
+
There are several data types in Python. To identify the data type we use the _type_built-in function. I would like to ask you to focus on understanding different data types very well. When it comes to programming, it is all about data types. I introduced data types at the very beginning and it comes again, because every topic is related to data types. We will cover data types in more detail in their respective sections.
174
179
175
180
## Checking Data types and Casting
176
181
177
182
- Check Data types: To check the data type of certain data/variable we use the _type_
178
-
**Example:**
183
+
**Examples:**
179
184
180
185
```py
181
186
# Different python data types
@@ -188,32 +193,31 @@ city= 'Helsinki' # str
188
193
age =250# int, it is not my real age, don't worry about it
- Casting: Converting one data type to another data type. We use _int()_, _float()_, _str()_, _list_
204
-
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 string, the number should be first converted to a string. We will talk about concatenation in String section.
205
-
**Example:**
208
+
- Casting: Converting one data type to another data type. We use _int()_, _float()_, _str()_, _list_, _set_
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
+
211
+
**Examples:**
206
212
207
213
```py
208
214
# int to float
209
-
210
215
num_int =10
211
216
print('num_int',num_int) # 10
212
217
num_float =float(num_int)
213
218
print('num_float:', num_float) # 10.0
214
219
215
220
# float to int
216
-
217
221
gravity =9.81
218
222
print(int(gravity)) # 9
219
223
@@ -223,22 +227,25 @@ print(num_int) # 10
223
227
num_str =str(num_int)
224
228
print(num_str) # '10'
225
229
226
-
# str to int
230
+
# str to int or float
227
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
1. Integers: Integer(negative, zero and positive) numbers
244
251
Example:
@@ -252,7 +259,7 @@ Number data types in python:
252
259
Example:
253
260
1 + j, 2 + 4j, 1 - 1j
254
261
255
-
🌕 You are awesome. You have just completed day 2 challenges and you are two steps ahead on your way to greatness. Now do some exercises for your brain and for your muscle.
262
+
🌕 You are awesome. You have just completed day 2 challenges and you are two steps ahead on your way to greatness. Now do some exercises for your brain and muscles.
256
263
257
264
## 💻 Exercises - Day 2
258
265
@@ -275,22 +282,22 @@ Number data types in python:
275
282
### Exercises: Level 2
276
283
277
284
1. Check the data type of all your variables using type() built-in function
278
-
1. Using the _len()_ built-in function find the length of your first name
279
-
1. Compare the length of your first name and your last name
280
-
1. Declare 5 as num_one and 4 as num_two
281
-
1. Add num_one and num_two and assign the value to a variable \_total
282
-
2. Subtract num_two from num_one and assign the value to a variable \_diff
283
-
3. Multiply num_two and num_one and assign the value to a variable \_product
284
-
4. Divide num_one by num_two and assign the value to a variable \_division
285
-
5. Use modulus division to find num_two divided by num_one and assign the value to a variable \_remainder
286
-
6. Calculate num_one to the power of num_two and assign the value to a variable \_exp
287
-
7. Find floor division of num_one by num_two and assign the value to a variable \_floor_division
288
-
1. The radius of a circle is 30 meters.
289
-
1. Calculate the area of a circle and assign the value to a variable _area_of_circle_
290
-
2. Calculate the circumference of a circle and assign the value to a variable _circum_of_circle_
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.
296
+
1. Calculate the area of a circle and assign the value to a variable name of _area_of_circle_
297
+
2. Calculate the circumference of a circle and assign the value to a variable name of _circum_of_circle_
291
298
3. Take radius as user input and calculate the area.
292
-
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
293
-
1. Run help('keywords') in python shell or in your file to check for the reserved words
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
0 commit comments