mirror of
https://github.com/Asabeneh/30-Days-Of-Python.git
synced 2026-06-12 21:01:48 +08:00
day 2
This commit is contained in:
commit
4a288e2859
@ -20,30 +20,30 @@
|
||||

|
||||
|
||||
- [📘 Day 2](#%f0%9f%93%98-day-2)
|
||||
- [Built in functions](#built-in-functions)
|
||||
- [Built-in functions](#built-in-functions)
|
||||
- [Variables](#variables)
|
||||
- [Data Types](#data-types)
|
||||
- [Checking Data types and Casting](#checking-data-types-and-casting)
|
||||
- [Number](#number)
|
||||
- [Numbers](#number)
|
||||
- [💻 Exercises - Day 2](#%f0%9f%92%bb-exercises---day-2)
|
||||
|
||||
# 📘 Day 2
|
||||
|
||||
## Built in functions
|
||||
|
||||
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).
|
||||
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).
|
||||
|
||||

|
||||

|
||||
|
||||
Let's open the python shell and start using some of the most common built in functions.
|
||||
Let's open the python shell and start using some of the most common built-in functions.
|
||||
|
||||

|
||||

|
||||
|
||||
Let's practice more by using different built-in functions
|
||||
|
||||

|
||||
|
||||
As you can see from the above terminal, python has reserved words. We do not use reserved words to declare variables or functions. We will cover variables in the next section.
|
||||
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.
|
||||
|
||||
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
|
||||
|
||||
@ -51,8 +51,8 @@ I believe, by now you are familiar with built-in functions. Let's do one more pr
|
||||
|
||||
## Variables
|
||||
|
||||
Variables store data in a computer memory. Mnemonic variables are recommend to use in many programming languages. A variable refers to an a memory address in which a data is stored.
|
||||
Number at the beginning, special character, hyphen are not allowed. A variable can have a short name (like x,y,z) but a more descriptive name (firstname, lastname, age, country) is highly recommended .
|
||||
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.
|
||||
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.
|
||||
Python Variable Name Rules
|
||||
|
||||
- A variable name must start with a letter or the underscore character
|
||||
@ -89,7 +89,7 @@ num-1
|
||||
|
||||
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.
|
||||
|
||||
When we assign a certain data type to a variable 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.
|
||||
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.
|
||||
|
||||
_Example:_
|
||||
|
||||
@ -140,7 +140,7 @@ print('Skills: ', skills)
|
||||
print('Person information: ', person_info)
|
||||
```
|
||||
|
||||
Variable can also be declared in one line:
|
||||
Variables can also be declared in one line:
|
||||
|
||||
**Example:**
|
||||
|
||||
@ -168,16 +168,16 @@ print(age)
|
||||
|
||||
## Data Types
|
||||
|
||||
There are several data types in python. To identify the data type we use the _type_ builtin function. I like you to focus 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.
|
||||
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.
|
||||
|
||||
## Checking Data types and Casting
|
||||
|
||||
- Check Data types: To check the data type of a certain data type we use the _type_
|
||||
- Check Data types: To check the data type of certain data/variable we use the _type_
|
||||
**Example:**
|
||||
|
||||
```py
|
||||
# Different python data types
|
||||
# Let's declare different data types
|
||||
# Let's declare variables with various data types
|
||||
|
||||
first_name = 'Asabeneh' # str
|
||||
last_name = 'Yetayeh' # str
|
||||
@ -199,7 +199,7 @@ print(type(zip([1,2],[3,4]))) # set
|
||||
```
|
||||
|
||||
- Casting: Converting one data type to another data type. We use _int()_, _float()_, _str()_, _list_
|
||||
When we do arithmetic operations string numbers should be first converted to int or float if not it returns 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.
|
||||
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.
|
||||
**Example:**
|
||||
|
||||
```py
|
||||
@ -234,15 +234,15 @@ first_name_to_list = list(first_name)
|
||||
print(first_name_to_list) # ['A', 's', 'a', 'b', 'e', 'n', 'e', 'h']
|
||||
```
|
||||
|
||||
## Number
|
||||
## Numbers
|
||||
|
||||
Numbers are python data types.
|
||||
Number data types in python:
|
||||
|
||||
1. Integers: Integer(negative, zero and positive) numbers
|
||||
Example:
|
||||
... -3, -2, -1, 0, 1, 2, 3 ...
|
||||
|
||||
2. Floating Numbers(Decimal numbers)
|
||||
2. Floating Point Numbers(Decimal numbers)
|
||||
Example:
|
||||
... -3.5, -2.25, -1.0, 0.0, 1.1, 2.2, 3.5 ...
|
||||
|
||||
@ -252,8 +252,8 @@ Numbers are python data types.
|
||||
|
||||
## 💻 Exercises - Day 2
|
||||
|
||||
1. Inside 30DaysOfPython create a folder called day_2. Inside this folder create a file name called variables.py
|
||||
2. Writ a python comment saying 'Day 2: 30 Days of python programming'
|
||||
1. Inside 30DaysOfPython create a folder called day_2. Inside this folder create a file named variables.py
|
||||
2. Write a python comment saying 'Day 2: 30 Days of python programming'
|
||||
3. Declare a first name variable and assign a value to it
|
||||
4. Declare a last name variable and assign a value to it
|
||||
5. Declare a full name variable and assign a value to it
|
||||
@ -265,23 +265,23 @@ Numbers are python data types.
|
||||
11. Declare a variable is_true and assign a value to it
|
||||
12. Declare a variable is_light_on and assign a value to it
|
||||
13. Declare multiple variable on one line
|
||||
14. Check the data type of all your variables using type() built in function
|
||||
14. Check the data type of all your variables using type() built-in function
|
||||
15. Using the _len()_ built-in function find the length of your first name
|
||||
16. Compare the length of your first name and your last name
|
||||
17. Declare 5 as num_one and 4 as num_two
|
||||
1. Add num*one and num_two and assign the value to a variable \_total*
|
||||
2. Subtract num*two from num_one and assign the value to a variable \_diff*
|
||||
3. Multiply num*two and num_one and assign the value to a variable \_product*
|
||||
4. Divide num*one by num_two and assign the value to a variable \_division*
|
||||
5. Use modulus division to find num*two divided by num_one and assign the value to a variable \_remainder*
|
||||
6. Calculate num*one the power of num_two and assign the value to a variable \_exp*
|
||||
7. Find floor division of num*one by num_two and assign the value to a variable \_floor_division*
|
||||
1. Add num_one and num_two and assign the value to a variable \_total
|
||||
2. Subtract num_two from num_one and assign the value to a variable \_diff
|
||||
3. Multiply num_two and num_one and assign the value to a variable \_product
|
||||
4. Divide num_one by num_two and assign the value to a variable \_division
|
||||
5. Use modulus division to find num_two divided by num_one and assign the value to a variable \_remainder
|
||||
6. Calculate num_one to the power of num_two and assign the value to a variable \_exp
|
||||
7. Find floor division of num_one by num_two and assign the value to a variable \_floor_division
|
||||
18. The radius of a circle is 30 meters.
|
||||
1. Calculate the area of a circle and assign the value to a variable _area_of_circle_
|
||||
2. Calculate the circumference of a circle and assign the value to a variable _circum_of_circle_
|
||||
3. Take radius as user input and calculate the area.
|
||||
19. 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
|
||||
20. Run help('keywords') on python shell or in your file check the reserved words
|
||||
20. Run help('keywords') in python shell or in your file to check for the reserved words
|
||||
|
||||
🎉 CONGRATULATIONS ! 🎉
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user