Skip to content

Commit

Permalink
day to second version
Browse files Browse the repository at this point in the history
  • Loading branch information
Asabeneh committed Jul 6, 2021
1 parent 73db72a commit 770d822
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<sub>Author:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
<small> First Edition: Nov 22 - Dec 22, 2019</small>
<small> Second Edition: July, 2020</small>
</sub>

</div>
Expand All @@ -22,6 +22,7 @@
- [📘 Day 2](#-day-2)
- [Built in functions](#built-in-functions)
- [Variables](#variables)
- [Declaring Multiple Variable in a Line](#declaring-multiple-variable-in-a-line)
- [Data Types](#data-types)
- [Checking Data types and Casting](#checking-data-types-and-casting)
- [Numbers](#numbers)
Expand All @@ -33,36 +34,37 @@

## 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 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/2/library/functions.html).

![Built-in Functions](../images/builtin-functions.png)

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

![Built-in functions](../images/builtin-functions_practice.png)

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

![Help and Dir Built in Functions](../images/help_and_dir_builtin.png)

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.
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
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.

![Min Max Sum](../images/builtin-functional-final.png)

## Variables

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.
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.
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.

Python Variable Name Rules

- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and \_ )
- Variable names are case-sensitive (firstname, Firstname, FirstName and FIRSTNAME are different variables)
- Variable names are case-sensitive (firstname, Firstname, FirstName and FIRSTNAME) are different variables)

Valid variable names
Let us se valid variable names

```shell
firstname
Expand All @@ -74,9 +76,10 @@ first_name
last_name
capital_city
_if # if we want to use reserved word as a variable
year_2019
year2019
current_year_2019
year_2021
year2021
current_year_2021
birth_year
num1
num2
```
Expand All @@ -85,19 +88,20 @@ Invalid variables names

```shell
first-name
first@name
first$name
num-1
1num
```

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.
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.

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.
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.

_Example:_

```py
# Variables in Python

first_name = 'Asabeneh'
last_name = 'Yetayeh'
country = 'Finland'
Expand All @@ -113,17 +117,17 @@ person_info = {
}
```

Let's use _print()_ and _len()_ built in 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.
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.

**Example:**

```py
print('Hello, World!')
print('Hello',',', 'World','!') # it can take multiple arguments
print('Hello, World!') # The text Hello, World! is an argument
print('Hello',',', 'World','!') # it can take multiple arguments, four arguments have been passed
print(len('Hello, World!')) # it takes only one argument
```

Let's print and also find the length of the variables declared at the top:
Let us print and also find the length of the variables declared at the top:

**Example:**

Expand All @@ -142,7 +146,9 @@ print('Skills: ', skills)
print('Person information: ', person_info)
```

Variables can also be declared in one line:
### Declaring Multiple Variable in a Line

Multiple variables can also be declared in one line:

**Example:**

Expand All @@ -157,7 +163,7 @@ print('Age: ', age)
print('Married: ', is_married)
```

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.
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.
**Example:**

```py
Expand All @@ -170,7 +176,7 @@ print(age)

## Data Types

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.
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.

## Checking Data types and Casting

Expand All @@ -194,26 +200,25 @@ print(type(10)) # int
print(type(3.14)) # float
print(type(1 + 1j)) # complex
print(type(True)) # bool
print(type([1, 2,3,4])) # list
print(type([1, 2, 3, 4])) # list
print(type({'name':'Asabeneh','age':250, 'is_married':250})) # dict
print(type((1,2))) # tuple
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 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.
- Casting: Converting one data type to another data type. We use _int()_, _float()_, _str()_, _list_, _set_
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.

**Example:**

```py
# int to float

num_int = 10
print('num_int',num_int) # 10
num_float = float(num_int)
print('num_float:', num_float) # 10.0

# float to int

gravity = 9.81
print(int(gravity)) # 9

Expand All @@ -223,7 +228,7 @@ print(num_int) # 10
num_str = str(num_int)
print(num_str) # '10'

# str to int
# str to int or float
num_str = '10.6'
print('num_int', int(num_str)) # 10
print('num_float', float(num_str)) # 10.6
Expand All @@ -238,7 +243,7 @@ print(first_name_to_list) # ['A', 's', 'a', 'b', 'e', 'n', 'e', 'h']

## Numbers

Number data types in python:
Number data types in Python:

1. Integers: Integer(negative, zero and positive) numbers
Example:
Expand All @@ -252,7 +257,7 @@ Number data types in python:
Example:
1 + j, 2 + 4j, 1 - 1j

🌕 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.
🌕 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.

## 💻 Exercises - Day 2

Expand All @@ -275,22 +280,22 @@ Number data types in python:
### Exercises: Level 2

1. Check the data type of all your variables using type() built-in function
1. Using the _len()_ built-in function find the length of your first name
1. Using the _len()_ built-in function, find the length of your first name
1. Compare the length of your first name and your last name
1. 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 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
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
1. 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_
1. Calculate the area of a circle and assign the value to a variable name of _area_of_circle_
2. Calculate the circumference of a circle and assign the value to a variable name of _circum_of_circle_
3. Take radius as user input and calculate the area.
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
1. Run help('keywords') in python shell or in your file to check for the reserved words
1. Run help('keywords') in Python shell or in your file to check for the Python reserved words or keywords

🎉 CONGRATULATIONS ! 🎉

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

<sub>Author:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
<small> First Edition: Nov 22 - Dec 22, 2019</small>
<small> Second Edition: July, 2021</small>
</sub>
</div>

Expand Down

0 comments on commit 770d822

Please sign in to comment.