mirror of
https://github.com/Asabeneh/30-Days-Of-Python.git
synced 2026-06-03 21:02:42 +08:00
day 3
This commit is contained in:
parent
77eef5b6a4
commit
dbd62212e7
BIN
images/arithmetic_operators.png
Normal file
BIN
images/arithmetic_operators.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 42 KiB |
BIN
images/assignmet_operators.png
Normal file
BIN
images/assignmet_operators.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 59 KiB |
BIN
images/comparison_operators.png
Normal file
BIN
images/comparison_operators.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
BIN
images/identity_operators.png
Normal file
BIN
images/identity_operators.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
BIN
images/logical_operators.png
Normal file
BIN
images/logical_operators.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 44 KiB |
392
readme.md
392
readme.md
@ -1,7 +1,7 @@
|
||||
|
||||
|
||||

|
||||
## Table of Content
|
||||
## Table of Contents
|
||||
- [Day 1](#day-1)
|
||||
- [Welcome](#welcome)
|
||||
- [Introduction](#introduction)
|
||||
@ -30,10 +30,17 @@
|
||||
- [Built in functions](#built-in-functions)
|
||||
- [Variables](#variables)
|
||||
- [Data Types](#data-types)
|
||||
- [Number](#number-1)
|
||||
- [String](#string-1)
|
||||
- [Boolean](#boolean)
|
||||
- [Checking Data types and Casting](#checking-data-types-and-casting)
|
||||
- [Number](#number-1)
|
||||
- [Exercises - Day 2](#exercises---day-2)
|
||||
- [Day 3](#day-3)
|
||||
- [Boolean](#boolean)
|
||||
- [Operators:](#operators)
|
||||
- [Arithmetic Operators:](#arithmetic-operators)
|
||||
- [Comparison Operators](#comparison-operators)
|
||||
- [Logical Operators](#logical-operators)
|
||||
- [Exercises - Day 3](#exercises---day-3)
|
||||
- [Part 1](#part-1)
|
||||
|
||||
# Day 1
|
||||
## Welcome
|
||||
@ -262,21 +269,21 @@ The python interactive shell was printing without using **print** but on visual
|
||||
helloworld.py
|
||||
```py
|
||||
# Day 1 - 30DaysOfPython Challenge
|
||||
print(2 + 3) # addition(+)
|
||||
print(3 - 1) # subtraction(-)
|
||||
print(2 * 3) # multiplication(*)
|
||||
print(3 / 2) # division(/)
|
||||
print(3 ** 2) # exponential(**)
|
||||
print(3 % 2) # modulus(%)
|
||||
print(3 // 2) # Floor division operator(//)
|
||||
print(2 + 3) # addition(+)
|
||||
print(3 - 1) # subtraction(-)
|
||||
print(2 * 3) # multiplication(*)
|
||||
print(3 / 2) # division(/)
|
||||
print(3 ** 2) # exponential(**)
|
||||
print(3 % 2) # modulus(%)
|
||||
print(3 // 2) # Floor division operator(//)
|
||||
# Checking data types
|
||||
print(type(10)) # Int
|
||||
print(type(3.14)) # Float
|
||||
print(type(1 + 3j)) # Complex number
|
||||
print(type('Asabeneh')) # String
|
||||
print(type([1, 2, 3])) # List
|
||||
print(type({'name':'Asabeneh'})) #Dictionary
|
||||
print(type({9.8, 3.14, 2.7})) #Tuple
|
||||
print(type({'name':'Asabeneh'})) # Dictionary
|
||||
print(type({9.8, 3.14, 2.7})) # Tuple
|
||||
```
|
||||

|
||||
|
||||
@ -312,7 +319,7 @@ In python we have lots of built in functions. Built-in functions are globally av
|
||||
|
||||

|
||||
|
||||
Let's open the python shell and start using the 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.
|
||||
|
||||

|
||||
|
||||
@ -396,7 +403,6 @@ person_info = {
|
||||
|
||||
Let's print and also find the length of the variables declared at the top:
|
||||
|
||||
|
||||
**Example:**
|
||||
```py
|
||||
# Printing the values stored in the variables
|
||||
@ -433,31 +439,71 @@ print('Married: ', is_married)
|
||||
```
|
||||
|
||||
## Data Types
|
||||
Different data types in python. There are different data type in python programming. To identify the data type we use the type method. In this section, we will see data types in detail.
|
||||
|
||||
**Example:**
|
||||
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.
|
||||
## Checking Data types and Casting
|
||||
* Check Data types: To check the data type of a certain data type we use the *type*
|
||||
**Example:**
|
||||
```py
|
||||
# Different python data types
|
||||
# Different python data types
|
||||
# Let's declare different data types
|
||||
|
||||
first_name = 'Asabeneh' # String
|
||||
last_name = 'Yetayeh' # String
|
||||
country = 'Finland' # String
|
||||
city= 'Helsinki'
|
||||
age = 250 # Number, it is not my real age, don't worry about it
|
||||
first_name = 'Asabeneh' # str
|
||||
last_name = 'Yetayeh' # str
|
||||
country = 'Finland' # str
|
||||
city= 'Helsinki' # str
|
||||
age = 250 # int, it is not my real age, don't worry about it
|
||||
|
||||
print(type('Asabeneh'))
|
||||
print(type(first_name))
|
||||
print(type(10))
|
||||
print(type(3.14))
|
||||
print(type(1 + 1j))
|
||||
print(type(True))
|
||||
print(type([1, 2,3,4]))
|
||||
print(type({'name':'Asabeneh','age':250, 'is_married':250}))
|
||||
print(type((1,2)))
|
||||
print(type(zip([1,2],[3,4])))
|
||||
```
|
||||
### Number
|
||||
# Printing out types
|
||||
print(type('Asabeneh')) # str
|
||||
print(type(first_name)) # str
|
||||
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({'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 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.
|
||||
**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
|
||||
|
||||
# int to str
|
||||
num_int = 10
|
||||
print(num_int) # 10
|
||||
num_str = str(num_int)
|
||||
print(num_str) # '10'
|
||||
|
||||
# str to int
|
||||
num_str = '10.6'
|
||||
print('num_int', int(num_str)) # 10
|
||||
print('num_float', float(num_str)) # 11
|
||||
|
||||
# str to list
|
||||
first = 'Asabeneh'
|
||||
print(first_name)
|
||||
print(first_name) # 'Asabeneh'
|
||||
first_name_to_list = list(first_name)
|
||||
print(first_name_to_list) # ['A', 's', 'a', 'b', 'e', 'n', 'e', 'h']
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Number
|
||||
Numbers are python data types.
|
||||
1. Integers: Integer(negative, zero and positive) numbers
|
||||
|
||||
Example:
|
||||
@ -472,58 +518,6 @@ print(type(zip([1,2],[3,4])))
|
||||
1 + j, 2 + 4j, 1 - 1j
|
||||
|
||||
|
||||
|
||||
Numbers are python data types.
|
||||
Arithmetic Operators: +, -, *, /
|
||||
```py
|
||||
# Arithmetic Operations in Python
|
||||
# Integers
|
||||
|
||||
print('Addition: ', 1 + 2)
|
||||
print('Subtraction: ', 2 - 1)
|
||||
print('Multiplication: ', 2 * 3)
|
||||
print ('Division: ', 4 / 2) # Division in python gives floating number
|
||||
print('Division: ', 6 / 2)
|
||||
print('Division: ', 7 / 2)
|
||||
print('Division without the remainder: ', 7 // 2) # gives without the floating number or without the remaining
|
||||
print('Modulus: ', 3 % 2) # Gives the remainder
|
||||
print ('Division without the remainder: ',7 // 3)
|
||||
print('Exponential: ', 3 ** 2) # it means 3 * 3
|
||||
# Floating numbers
|
||||
print('Floating Number', 3.14)
|
||||
# Complex numbers
|
||||
print('Complex number: ', 1+1j)
|
||||
print('Multiplying complex number: ',(1+1j) * (1-1j))
|
||||
|
||||
|
||||
print('== Addition, Subtraction, Multiplication, Division, Modules ==')
|
||||
|
||||
num_one = 3
|
||||
num_two = 4
|
||||
|
||||
total = num_one + num_two
|
||||
diff = num_two - num_one
|
||||
product = num_one * num_two
|
||||
div = num_two / num_two
|
||||
remainder = num_two % num_one
|
||||
|
||||
print('sum: ', total)
|
||||
print('difference: ', diff)
|
||||
print('product: ', product)
|
||||
print('division: ', div)
|
||||
print('remainder: ', remainder)
|
||||
|
||||
mass = 75
|
||||
gravity = 9.81
|
||||
# Calculate the weight of the object on planet earth
|
||||
weight = mass * gravity
|
||||
|
||||
print(weight, 'N')
|
||||
```
|
||||
|
||||
## String
|
||||
## Boolean
|
||||
|
||||
## 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'
|
||||
@ -555,6 +549,228 @@ print(weight, 'N')
|
||||
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
|
||||
|
||||
|
||||
# Day 3
|
||||
## Boolean
|
||||
|
||||
A boolean data type represents one of the two values:*True* or *False*. The use of these data types will be clear when you start the comparison operator. The first letter **T** for True and **F** for False should be capital unlike JavaScript.
|
||||
**Example: Boolean Values**
|
||||
```py
|
||||
print(True)
|
||||
print(False)
|
||||
```
|
||||
## Operators:
|
||||
Python language supports several types of operators. In this section, we will focus on few them.
|
||||
### Arithmetic Operators:
|
||||
* Addition(+): a + b
|
||||
* Subtraction(-): a -b
|
||||
* Multiplication(*):a * b
|
||||
* Division(/): a / b
|
||||
* Modulus(%):a % b
|
||||
* Floor division(//): a // b
|
||||
* Exponential(**):a ** b
|
||||
|
||||

|
||||
|
||||
**Example:Integers**
|
||||
```py
|
||||
# Arithmetic Operations in Python
|
||||
# Integers
|
||||
|
||||
print('Addition: ', 1 + 2)
|
||||
print('Subtraction: ', 2 - 1)
|
||||
print('Multiplication: ', 2 * 3)
|
||||
print ('Division: ', 4 / 2) # Division in python gives floating number
|
||||
print('Division: ', 6 / 2)
|
||||
print('Division: ', 7 / 2)
|
||||
print('Division without the remainder: ', 7 // 2) # gives without the floating number or without the remaining
|
||||
print('Modulus: ', 3 % 2) # Gives the remainder
|
||||
print ('Division without the remainder: ',7 // 3)
|
||||
print('Exponential: ', 3 ** 2) # it means 3 * 3
|
||||
```
|
||||
**Example:Floats**
|
||||
```py
|
||||
# Floating numbers
|
||||
print('Floating Number,PI', 3.14)
|
||||
print('Floating Number, gravity', 9.81)
|
||||
|
||||
# Complex numbers
|
||||
print('Complex number: ', 1+1j)
|
||||
print('Multiplying complex number: ',(1+1j) * (1-1j))
|
||||
```
|
||||
Let's declare a variable and assign a number data type. I am going to use single character variable but remember do not develop a habit of declaring such types of variable. Variable names should be all the time mnemonic.
|
||||
|
||||
**Example:**
|
||||
|
||||
```python
|
||||
|
||||
# Declaring the variable at the top first
|
||||
|
||||
a = 3 # a is a variable name and 3 is an integer data type
|
||||
b = 2 # b is a variable name and 3 is an integer data type
|
||||
|
||||
# Arithmetic operations and assigning the result to a avariable
|
||||
total = a + b
|
||||
diff = a - b
|
||||
product = a * b
|
||||
division = a / b
|
||||
remainder = a % b
|
||||
floor_division = a // b
|
||||
exponential = a ** b
|
||||
|
||||
# I should have used sum instead of total but sum is a built-in function try to avoid over ridding builtin functions
|
||||
print(total) # if you don't label your print with some string, you never know from where is the result is coming
|
||||
print('a + b = ', total)
|
||||
print('a - b = ', diff)
|
||||
print('a * b = ', product)
|
||||
print('a / b = ', division)
|
||||
print('a % b = ', remainder)
|
||||
print('a // b = ', floor_division)
|
||||
print('a ** b = ', exponential)
|
||||
```
|
||||
|
||||
**Example:**
|
||||
```py
|
||||
print('== Addition, Subtraction, Multiplication, Division, Modulus ==')
|
||||
|
||||
# Declaring values and organizing them together
|
||||
num_one = 3
|
||||
num_two = 4
|
||||
|
||||
# Arithmetic operations
|
||||
total = num_one + num_two
|
||||
diff = num_two - num_one
|
||||
product = num_one * num_two
|
||||
div = num_two / num_two
|
||||
remainder = num_two % num_one
|
||||
|
||||
# Printing values with label
|
||||
print('total: ', total)
|
||||
print('difference: ', diff)
|
||||
print('product: ', product)
|
||||
print('division: ', div)
|
||||
print('remainder: ', remainder)
|
||||
```
|
||||
|
||||
Let's start start connecting the dots and start making use of what we knew to calculate(area, volume, weight, perimeter, distance, force)
|
||||
|
||||
**Example:**
|
||||
```py
|
||||
# Calculating area of a circle
|
||||
radius = 10 # radius of a circle
|
||||
area_of_circle = 3.14 * radius ** 2 # two * sign means exponent or power
|
||||
print('Area of a circle:', area_of_circle)
|
||||
|
||||
# Calculating area of a rectangle
|
||||
length = 10
|
||||
width = 20
|
||||
area_of_rectangle = length * width
|
||||
print('Area of rectangle:', area_of_width)
|
||||
|
||||
# Calculating a weight of an object
|
||||
mass = 75
|
||||
gravity = 9.81
|
||||
weight = mass * gravity
|
||||
print(weight, 'N') # Adding unit to the weight
|
||||
```
|
||||
|
||||
|
||||
### Comparison Operators
|
||||
In programming we compare values, we use comparison operators to compare two values. We check if a value is greater or less or equal to other value. The following table shows python comparison operators which was taken from [w3shool](https://www.w3schools.com/python/python_operators.asp).
|
||||
|
||||

|
||||
**Example: Comparison Operators**
|
||||
```py
|
||||
print(3 > 2) # True, because 3 is greater than 2
|
||||
print(3 >= 2) # True, because 3 is greater than 2
|
||||
print(3 < 2) # False, because 3 is greater than 2
|
||||
print(2 < 3) # True, because 2 is less than 3
|
||||
print(2 <= 3) # True, because 2 is less than 3
|
||||
print(3 == 2) # False, because 3 is not equal to 2
|
||||
print(3 != 2) # True, because 3 is not equal to 2
|
||||
print(len('mango') == len('avocado')) # False
|
||||
print(len('mango') != len('avocado')) # True
|
||||
print(len('mango') < len('avocado')) # False
|
||||
print(len('milk') != len('meat')) # False
|
||||
print(len('milk') == len('meat')) # False
|
||||
print(len('tomato') == len('potato')) # True
|
||||
print(len('python') > len('dragon'))
|
||||
|
||||
|
||||
|
||||
|
||||
# Comparing something give either a True or False
|
||||
print('True == True: ', True == True)
|
||||
print('True == False: ', True == False)
|
||||
print('False == False:', False == False)
|
||||
print('True and True: ', True and True)
|
||||
print('True or False:', True or False)
|
||||
|
||||
|
||||
```
|
||||
In addition to the above comparison operator python uses:
|
||||
* *is*: Returns true if both variables are the same object(x is y)
|
||||
* *is not*: Returns true if both variables are not the same object(x is not y)
|
||||
* *in*: Returns True if a list with the a certain element(x in y)
|
||||
* *not in*: Returns True if a list doesn't have the a certain element(x in y)
|
||||
|
||||
```py
|
||||
print('1 is 1', 1 is 1) # True - because the data values are the same
|
||||
print('1 is not 2', 1 is not 2) # True - because 1 is not 2
|
||||
print('A in Asabeneh', 'A' in 'Asabeneh') # True - A found in the string
|
||||
print('B in Asabeneh', 'B' in 'Asabeneh') # False -there is lowercase be in the string but not uppercase B
|
||||
print('coding' in 'coding for all') # True - because the coding for all has the word coding
|
||||
print('a in an:', 'a' in 'an') # True
|
||||
print('4 is 2 ** 2:', 4 is 2 **2) # True
|
||||
|
||||
|
||||
```
|
||||
### Logical Operators
|
||||
Unlike other programming languages python uses the key word *and*, *or* and *not* for logical operator. Logical operators are used to combine conditional statements:
|
||||
|
||||

|
||||
|
||||
```py
|
||||
print(3 > 2 and 4 > 3) # True - because both statements are true
|
||||
print(3 > 2 and 4 < 3) # False - because the second statement is false
|
||||
print(3 < 2 and 4 < 3) # False - because both statements are false
|
||||
print(3 > 2 or 4 > 3) # True - because both statements are true
|
||||
print(3 > 2 or 4 < 3) # True - because one of the statement is true
|
||||
print(3 < 2 or 4 < 3) # False - because both statements are false
|
||||
print(not 3 > 2) # False - because 3 > 2 is true, then not True gives False
|
||||
print(not True) # False - Negation, the not operator turns true to false
|
||||
print(not False) # True
|
||||
print(not not True) # True
|
||||
print(not not False) # False
|
||||
|
||||
```
|
||||
|
||||
## Exercises - Day 3
|
||||
### Part 1
|
||||
1. Declare your age as integer variable
|
||||
2. Declare your height as a float variable
|
||||
3. Declare a complex number variable
|
||||
4. Calculate an area of a triangle (area = 0.5 x b x h)
|
||||
5. Calculate the perimeter of triangle (perimeter = a + b + c)
|
||||
6. Calculate an of area rectangle (area = length x width)
|
||||
7. Calculate the perimeter of rectangle (perimeter = 2 x (length + width))
|
||||
8. Calculate the area of a circle (area = 3.14 x r x r)
|
||||
9. Calculate the circumference of a circle(c = 2 x pi x r) where pi = 3.14.
|
||||
10. Calculate the slope, x-intercept and y-intercept of y = 2x -2
|
||||
11. Slope is (m = y2-y1/x2-x1). Find the slope between point (2, 2) and point(6,10)
|
||||
12. Calculate the value of y (y = x2 + 6x + 9). Try to use different x value and figure out at what x value y is 0.
|
||||
13. Compare the slope of q10 and 11
|
||||
14. Find the length of python and jargon and make a falsy comparison statment.
|
||||
15. Use and operator to check if 'on' is found in both python and jargon
|
||||
16. *I hope this course is not full of jargon*. Use *in* operator to check if *jargon* is in the sentence.
|
||||
17. There is no 'on' in both dragon and python
|
||||
18. Find the length of the text *python* and convert the value to float and convert it to string
|
||||
19. Even numbers are divisible by 2 and the remainder is zero. How do you check if a number is even or not using python?
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user