mirror of
https://github.com/Asabeneh/30-Days-Of-Python.git
synced 2026-06-15 21:20:29 +08:00
day 15
This commit is contained in:
parent
e84a48a42a
commit
7c70911dbc
44
readme.md
44
readme.md
@ -1,8 +1,21 @@
|
||||
[Part 1](https://github.com/Asabeneh/30-Days-Of-Python) | [Part 2](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)| [Part 3](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)| [Part 4](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)| [Part 5](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
***
|
||||
|
||||

|
||||
- [Day 1](#day-1)
|
||||
|
||||
[Part 1: Day 1 - 3](https://github.com/Asabeneh/30-Days-Of-Python)
|
||||
[Part 2: Day 4 - 6](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)
|
||||
[Part 3: Day 7 - 9](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)
|
||||
[Part 4: Day 10 - 12](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)
|
||||
[Part 5: Day 13 - 15](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
[Part 6: Day 16 - 18](#)
|
||||
[Part 7: Day 19 - 21](#)
|
||||
[Part 8: Day 22 - 24](#)
|
||||
[Part 9: Day 25 - 27](#)
|
||||
[Part 10:Day 28 - 30](#)
|
||||
|
||||
---
|
||||
|
||||

|
||||
- [📘 Day 1](#%f0%9f%93%98-day-1)
|
||||
- [Welcome](#welcome)
|
||||
- [Introduction](#introduction)
|
||||
- [Why Python ?](#why-python)
|
||||
@ -25,24 +38,24 @@
|
||||
- [Set](#set)
|
||||
- [Checking Data types](#checking-data-types)
|
||||
- [Python File](#python-file)
|
||||
- [Exercises - Day 1](#exercises---day-1)
|
||||
- [Day 2](#day-2)
|
||||
- [💻 Exercises - Day 1](#%f0%9f%92%bb-exercises---day-1)
|
||||
- [📘 Day 2](#%f0%9f%93%98-day-2)
|
||||
- [Built in functions](#built-in-functions)
|
||||
- [Variables](#variables)
|
||||
- [Data Types](#data-types)
|
||||
- [Checking Data types and Casting](#checking-data-types-and-casting)
|
||||
- [Number](#number-1)
|
||||
- [Exercises - Day 2](#exercises---day-2)
|
||||
- [Day 3](#day-3)
|
||||
- [💻 Exercises - Day 2](#%f0%9f%92%bb-exercises---day-2)
|
||||
- [📘 Day 3](#%f0%9f%93%98-day-3)
|
||||
- [Boolean](#boolean)
|
||||
- [Operators:](#operators)
|
||||
- [Assignment Operators:](#assignment-operators)
|
||||
- [Arithmetic Operators:](#arithmetic-operators)
|
||||
- [Comparison Operators](#comparison-operators)
|
||||
- [Logical Operators](#logical-operators)
|
||||
- [Exercises - Day 3](#exercises---day-3)
|
||||
- [💻 Exercises - Day 3](#%f0%9f%92%bb-exercises---day-3)
|
||||
|
||||
# Day 1
|
||||
# 📘 Day 1
|
||||
## Welcome
|
||||
**Congratulations** for deciding to participate in a ***30 days of Python*** programming challenge . In this challenge you will learn everything you need to be a python programmer and the whole concepts of programming. In the end of the challenge you will get a ***30DaysOfPython*** programming challenge certificate. [Join the telegram channel](https://t.me/ThirtyDaysOfPython)
|
||||
## Introduction
|
||||
@ -288,7 +301,7 @@ print(type({9.8, 3.14, 2.7})) # Tuple
|
||||

|
||||
|
||||
|
||||
## Exercises - Day 1
|
||||
## 💻 Exercises - Day 1
|
||||
1. Check the python version you are using
|
||||
2. Open the python interactive shell and do the following operations. The operands are 3 and 4. Check the example above
|
||||
- addition(+)
|
||||
@ -314,7 +327,7 @@ print(type({9.8, 3.14, 2.7})) # Tuple
|
||||
- Your country
|
||||
3. Create a folder name day_1 inside 30DaysOfPython folder. Inside day_1 folder, create a file python file helloword.py and repeat question 1, 2, 3 and 4. Remember to use *print()* when you are working on a python file. Navigate to the directory where you saved your file, and run it.
|
||||
|
||||
# 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).
|
||||
|
||||
@ -334,7 +347,6 @@ I believe, by now you are familiar with built-in functions. Let's do one more
|
||||
|
||||

|
||||
|
||||
|
||||
## 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 .
|
||||
@ -498,8 +510,6 @@ 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
|
||||
@ -515,7 +525,7 @@ print(first_name_to_list) # ['A', 's', 'a', 'b', 'e', 'n', 'e', 'h']
|
||||
Example:
|
||||
1 + j, 2 + 4j, 1 - 1j
|
||||
|
||||
## Exercises - Day 2
|
||||
## 💻 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'
|
||||
3. Declare a first name variable and assign a value to it
|
||||
@ -547,7 +557,7 @@ print(first_name_to_list) # ['A', 's', 'a', 'b', 'e', 'n', 'e', 'h']
|
||||
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
|
||||
# 📘 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.
|
||||
@ -741,7 +751,7 @@ print(not not True) # True
|
||||
print(not not False) # False
|
||||
```
|
||||
|
||||
## Exercises - Day 3
|
||||
## 💻 Exercises - Day 3
|
||||
1. Declare your age as integer variable
|
||||
2. Declare your height as a float variable
|
||||
3. Declare a complex number variable
|
||||
|
||||
@ -1,9 +1,21 @@
|
||||
[Part 1](https://github.com/Asabeneh/30-Days-Of-Python) | [Part 2](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)| [Part 3](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)| [Part 4](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)| [Part 5](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
***
|
||||
|
||||

|
||||
|
||||
- [Day 10](#day-10)
|
||||
[Part 1: Day 1 - 3](https://github.com/Asabeneh/30-Days-Of-Python)
|
||||
[Part 2: Day 4 - 6](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)
|
||||
[Part 3: Day 7 - 9](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)
|
||||
[Part 4: Day 10 - 12](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)
|
||||
[Part 5: Day 13 - 15](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
[Part 6: Day 16 - 18](#)
|
||||
[Part 7: Day 19 - 21](#)
|
||||
[Part 8: Day 22 - 24](#)
|
||||
[Part 9: Day 25 - 27](#)
|
||||
[Part 10:Day 28 - 30](#)
|
||||
|
||||
---
|
||||

|
||||
|
||||
- [📘 Day 10](#%f0%9f%93%98-day-10)
|
||||
- [Loops](#loops)
|
||||
- [While Loop](#while-loop)
|
||||
- [Break and continue](#break-and-continue)
|
||||
@ -13,8 +25,8 @@
|
||||
- [Nested for loop](#nested-for-loop)
|
||||
- [For Else](#for-else)
|
||||
- [Pass](#pass)
|
||||
- [Exercises: Day 10](#exercises-day-10)
|
||||
- [Day 11](#day-11)
|
||||
- [💻 Exercises: Day 10](#%f0%9f%92%bb-exercises-day-10)
|
||||
- [📘 Day 11](#%f0%9f%93%98-day-11)
|
||||
- [Functions](#functions)
|
||||
- [Defining a Function](#defining-a-function)
|
||||
- [Declaring and calling a function](#declaring-and-calling-a-function)
|
||||
@ -27,8 +39,8 @@
|
||||
- [Arbitrary number of arguments](#arbitrary-number-of-arguments)
|
||||
- [Default and arbitrary number of parameters in function](#default-and-arbitrary-number-of-parameters-in-function)
|
||||
- [Function as parameter of other function](#function-as-parameter-of-other-function)
|
||||
- [Exercises: Day 11](#exercises-day-11)
|
||||
- [Day 12](#day-12)
|
||||
- [💻 Exercises: Day 11](#%f0%9f%92%bb-exercises-day-11)
|
||||
- [📘 Day 12](#%f0%9f%93%98-day-12)
|
||||
- [Module](#module)
|
||||
- [What is a module](#what-is-a-module)
|
||||
- [Creating a module](#creating-a-module)
|
||||
@ -41,8 +53,8 @@
|
||||
- [Statistics Module](#statistics-module)
|
||||
- [Math Module](#math-module)
|
||||
- [Random Module](#random-module)
|
||||
- [Exercises: Day 12](#exercises-day-12)
|
||||
# Day 10
|
||||
- [💻 Exercises: Day 12](#%f0%9f%92%bb-exercises-day-12)
|
||||
# 📘 Day 10
|
||||
## Loops
|
||||
Life is full of routines. In programming also we do lots of repetitive tasks. In order to handle repetitive task programming languages provide loops. Python programming language also provides the following types of two loops to handle looping.
|
||||
1. while loop
|
||||
@ -309,7 +321,7 @@ If we want to execute some message when the loop ends, we use else.
|
||||
### Pass
|
||||
In python after semicolon, it requires some code to run but we don't like to execute any code after if or for loop we can write the word *pass* to avoid error.
|
||||
|
||||
## Exercises: Day 10
|
||||
## 💻 Exercises: Day 10
|
||||
1. Iterate 0 to 10 using for loop, do the same using while and do while loop.
|
||||
2. Iterate 10 to 0 using for loop, do the same using while and do while loop.
|
||||
3. Write a loop that makes seven calls to print() output the following triangle:
|
||||
@ -361,7 +373,7 @@ In python after semicolon, it requires some code to run but we don't like to exe
|
||||
11. Go to the data folder and use the countries.py file. Loop through the countries and extract all the countries containing the word *land*.
|
||||
12. This is a fruit list, ['banana', 'orange', 'mango', 'lemon'] reverse the order using loop.
|
||||
|
||||
# Day 11
|
||||
# 📘 Day 11
|
||||
## Functions
|
||||
So far we have seen many builtin python functions. In this section, we will focus on custom functions. What is a function? Before we start making functions, lets understand what function is and why we need function?
|
||||
### Defining a Function
|
||||
@ -642,7 +654,7 @@ generate_groups('Team-1','Asabeneh','Brook','David','Eyob')
|
||||
return f(x)
|
||||
print(do_something(square_number, 3))
|
||||
```
|
||||
## Exercises: Day 11
|
||||
## 💻 Exercises: Day 11
|
||||
1. Declare a function *add_two_numbers* and it takes two two parameters and it returns sum.
|
||||
2. Area of a circle is calculated as follows: area = π x r x r. Write a function which calculates *area_of_circle*.
|
||||
3. Write a function called add_all_nums which take arbitrary number of arguments and sum all the arguments. Check if all the list items are number types. If not give return reasonable feedback.
|
||||
@ -681,7 +693,7 @@ Call your function *is_empty*, it takes a parameter and it checks if it is empty
|
||||
* Create a function called the most_spoken_languages the world and it returns the 10 or 20 most spoken countries in the world in descending order
|
||||
* Create a function called the most_populated_countries and it return 10 or 20 most populated countries in descending order.
|
||||
|
||||
# Day 12
|
||||
# 📘 Day 12
|
||||
## Module
|
||||
### What is a module
|
||||
A module is a file containing set of codes or a set of function which can be included to an application. A module could be a file containing a single variable, or function, a big code base.
|
||||
@ -819,7 +831,7 @@ By now you are familiar with importing modules. Lets do another more import to b
|
||||
print(random()) # it doesn't take argument and return 0 to 0.9999
|
||||
print(randint(5, 20)) # it returns a random number between 5 and 20
|
||||
```
|
||||
## Exercises: Day 12
|
||||
## 💻 Exercises: Day 12
|
||||
1. Writ a function which generates a six digit random_user_id.
|
||||
```py
|
||||
print(random_user_id());
|
||||
|
||||
410
readme13-15.md
410
readme13-15.md
@ -1,17 +1,27 @@
|
||||
[Part 1](https://github.com/Asabeneh/30-Days-Of-Python) | [Part 2](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)| [Part 3](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)| [Part 4](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)| [Part 5](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
|
||||
---
|
||||
|
||||

|
||||
|
||||
- [Day 13](#day-13)
|
||||
[Part 1: Day 1 - 3](https://github.com/Asabeneh/30-Days-Of-Python)
|
||||
[Part 2: Day 4 - 6](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)
|
||||
[Part 3: Day 7 - 9](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)
|
||||
[Part 4: Day 10 - 12](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)
|
||||
[Part 5: Day 13 - 15](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
[Part 6: Day 16 - 18](#)
|
||||
[Part 7: Day 19 - 21](#)
|
||||
[Part 8: Day 22 - 24](#)
|
||||
[Part 9: Day 25 - 27](#)
|
||||
[Part 10:Day 28 - 30](#)
|
||||
|
||||
---
|
||||
|
||||
- [📘 Day 13](#%f0%9f%93%98-day-13)
|
||||
- [List Comprehension](#list-comprehension)
|
||||
- [Lambda Function](#lambda-function)
|
||||
- [Creating a lambda function](#creating-a-lambda-function)
|
||||
- [Lambda function inside other function](#lambda-function-inside-other-function)
|
||||
- [Exercises: Day 13](#exercises-day-13)
|
||||
- [Day 14](#day-14)
|
||||
- [Higher Order Functions](#higher-order-functions)
|
||||
- [💻 Exercises: Day 13](#%f0%9f%92%bb-exercises-day-13)
|
||||
- [📘 Day 14](#%f0%9f%93%98-day-14)
|
||||
- [Higher Order Functions](#higher-order-functions)
|
||||
- [Function as a parameter](#function-as-a-parameter)
|
||||
- [Function as a return value](#function-as-a-return-value)
|
||||
- [Python closures](#python-closures)
|
||||
@ -23,9 +33,22 @@
|
||||
- [Python - Map Function](#python---map-function)
|
||||
- [Python - Filter Function](#python---filter-function)
|
||||
- [Python - Reduce Function](#python---reduce-function)
|
||||
- [Exercises: Day 14](#exercises-day-14)
|
||||
- [💻 Exercises: Day 14](#%f0%9f%92%bb-exercises-day-14)
|
||||
- [📘 Day 15](#%f0%9f%93%98-day-15)
|
||||
- [Python Error Types](#python-error-types)
|
||||
- [SyntaxError](#syntaxerror)
|
||||
- [NameError](#nameerror)
|
||||
- [IndexError](#indexerror)
|
||||
- [ModuleNotFoundError](#modulenotfounderror)
|
||||
- [AttributeError](#attributeerror)
|
||||
- [KeyError](#keyerror)
|
||||
- [TypeError](#typeerror)
|
||||
- [ImportError](#importerror)
|
||||
- [ValueError](#valueerror)
|
||||
- [ZeroDivisionError](#zerodivisionerror)
|
||||
- [💻 Exercises: Day 15](#%f0%9f%92%bb-exercises-day-15)
|
||||
|
||||
# Day 13
|
||||
# 📘 Day 13
|
||||
|
||||
## List Comprehension
|
||||
|
||||
@ -146,7 +169,7 @@ cube = power(2)(3) # 8
|
||||
two_power_of_five = power(2)(5) # 32
|
||||
```
|
||||
|
||||
## Exercises: Day 13
|
||||
## 💻 Exercises: Day 13
|
||||
|
||||
1. Filter only negative or zero in the list using list comprehension
|
||||
```py
|
||||
@ -197,9 +220,9 @@ two_power_of_five = power(2)(5) # 32
|
||||
```
|
||||
1. Write a lambda function which can solve slope or y-intercept.
|
||||
|
||||
# Day 14
|
||||
# 📘 Day 14
|
||||
|
||||
# Higher Order Functions
|
||||
## Higher Order Functions
|
||||
|
||||
In python functions are treated as first class citizens, allowing you to perform the following operations on functions:
|
||||
|
||||
@ -476,7 +499,7 @@ print(list(long_names)) # ['Asabeneh']
|
||||
|
||||
### Python - Reduce Function
|
||||
|
||||
The *reduce()* function is defined in the functools module and we should import it from this module.Like map and filter it takes two parameters, a function and an iterable. However, it doesn't return another iterable, instead it returns a single value.
|
||||
The _reduce()_ function is defined in the functools module and we should import it from this module.Like map and filter it takes two parameters, a function and an iterable. However, it doesn't return another iterable, instead it returns a single value.
|
||||
|
||||
**Example:2**
|
||||
|
||||
@ -489,12 +512,14 @@ total = reduce(add_two, numbers_str)
|
||||
print(total) # 15
|
||||
```
|
||||
|
||||
## Exercises: Day 14
|
||||
## 💻 Exercises: Day 14
|
||||
|
||||
```py
|
||||
countries = ['Estonia', 'Finland', 'Sweden', 'Denmark', 'Norway', 'Iceland']
|
||||
names = ['Asabeneh', 'Lidiya', 'Ermias', 'Abraham']
|
||||
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
```
|
||||
|
||||
1. Explain the difference between map, filter, and reduce.
|
||||
1. Explain the difference between higher order function, closure and decorator
|
||||
1. Define a call function before map, filter or reduce, see examples.
|
||||
@ -509,18 +534,357 @@ numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
1. Use filter to filter out countries containing six letters and more in the country list.
|
||||
1. Use filter to filter out country start with 'E'
|
||||
1. Chain two or more list iterators(eg. arr.map(callback).filter(callback).reduce(callback))
|
||||
1. Declare a function called getStringLists which takes an list as a parameter and then returns an list only with string items.
|
||||
1. Declare a function called get_string_lists which takes an list as a parameter and then returns an list only with string items.
|
||||
1. Use reduce to sum all the numbers in the numbers list.
|
||||
1. Use reduce to concatenate all the countries and to produce this sentence: Estonia, Finland, Sweden, Denmark, Norway, and IceLand are north European countries
|
||||
2. Declare a function called categorize_countries which returns an list of countries which have some common pattern(you find the countries list in this repository as countries.js(eg 'land', 'ia', 'island', 'stan')).
|
||||
3. Create a function which return an list of objects, which is the letter and the number of times the letter use to start with a name of a country.
|
||||
4. Declare a get_first_ten_countries function and return an list of ten countries. Use different functional programming to work on the countries.js list
|
||||
5. Declare a get_last_ten_countries function which which returns the last ten countries in the countries list.
|
||||
6. Find out which letter is used many times as initial for a country name from the countries list(eg. Finland, Fiji, France etc)
|
||||
7. Use the countries_data.py file information, in the data folder. Sort countries by name, by capital, by population
|
||||
8. Sort out the ten most spoken languages by location.
|
||||
9. Sort out the ten most populated countries.
|
||||
1. Declare a function called categorize_countries which returns an list of countries which have some common pattern(you find the [countries list](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/countries.py) in this repository as countries.js(eg 'land', 'ia', 'island', 'stan')).
|
||||
1. Create a function which return a list of dictionary, which is the letter and the number of times the letter used to start a name of a country.
|
||||
1. Declare a get_first_ten_countries function and return an list of ten countries from the countries.js list in the data folder.
|
||||
1. Declare a get_last_ten_countries function which which returns the last ten countries in the countries list.
|
||||
1. Find out which letter is used many times as initial for a country name from the [countries list](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/data/countries-data.py)(eg. Finland, Fiji, France etc)
|
||||
1. Use the countries_data.py file information, in the data folder.
|
||||
- Sort countries by name, by capital, by population
|
||||
- Sort out the ten most spoken languages by location.
|
||||
- Sort out the ten most populated countries.
|
||||
|
||||
# 📘 Day 15
|
||||
|
||||
## Python Error Types
|
||||
|
||||
When we write code it common that we make a typo or other common errors. Based on the type of error we make python raise a kind of error which suggests us to fix it by reading the feedback. Understanding different types of errors in a certain programming language can help us to debug our code quick and in return it makes us a better programmer.
|
||||
|
||||
Let's see the most common error types step by step by open our python interactive shell. Go to your you computer terminal and write, python, the python interactive shell will be opened.
|
||||
|
||||
### SyntaxError
|
||||
|
||||
**Example 1: SyntaxError**
|
||||
|
||||
```py
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> print 'hello world'
|
||||
File "<stdin>", line 1
|
||||
print 'hello world'
|
||||
^
|
||||
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?
|
||||
>>>
|
||||
```
|
||||
|
||||
As you can see we made a syntax error because we forgot to enclose the string with parenthesis and python is already suggested us to fix. Let's fix it.
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> print 'hello world'
|
||||
File "<stdin>", line 1
|
||||
print 'hello world'
|
||||
^
|
||||
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?
|
||||
>>> print('hello world')
|
||||
hello world
|
||||
>>>
|
||||
```
|
||||
|
||||
The error was a _SyntaxError_ and we fixed and our code executed. Let see more error types.
|
||||
|
||||
### NameError
|
||||
|
||||
**Example 1: NameError**
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> print(age)
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
NameError: name 'age' is not defined
|
||||
>>>
|
||||
```
|
||||
|
||||
As you can see from the above error, it says that name age is not defined. Yes, it is true. We did define an age variable but we were trying to print it as if we declare it. Now, lets fix this by declaring age variable and assigning with a value.
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> print(age)
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
NameError: name 'age' is not defined
|
||||
>>> age = 25
|
||||
>>> print(age)
|
||||
25
|
||||
>>>
|
||||
```
|
||||
|
||||
The type of error was a _NameError_. We debugged the error by defining the variable name.
|
||||
|
||||
### IndexError
|
||||
|
||||
**Example 1: IndexError**
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> numbers = [1, 2, 3, 4, 5]
|
||||
>>> numbers[5]
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
IndexError: list index out of range
|
||||
>>>
|
||||
```
|
||||
|
||||
In the above example, python raised an _IndexError_ because the list has only 0 to 4 indexes, so it was out of range.
|
||||
|
||||
### ModuleNotFoundError
|
||||
|
||||
**Example 1: ModuleNotFoundError**
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> import maths
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
ModuleNotFoundError: No module named 'maths'
|
||||
>>>
|
||||
```
|
||||
|
||||
In the above example, I added extra s to math deliberately and _ModuleNotFoundError_ was raised. Lets fix the error by removing the extra added s from math.
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> import maths
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
ModuleNotFoundError: No module named 'maths'
|
||||
>>> import math
|
||||
>>>
|
||||
```
|
||||
|
||||
We fixed the error by removing the extra s. Now let's use some of the function from math module.
|
||||
|
||||
### AttributeError
|
||||
|
||||
**Example 1: AttributeError**
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> import maths
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
ModuleNotFoundError: No module named 'maths'
|
||||
>>> import math
|
||||
>>> math.PI
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
AttributeError: module 'math' has no attribute 'PI'
|
||||
>>>
|
||||
```
|
||||
|
||||
As you can see, now again I made a mistake instead of pi, I tried to call a PI function from maths module that raise an attribute error, it means the function does not exist in the module. Lets fix it by change from PI to pi.
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> import maths
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
ModuleNotFoundError: No module named 'maths'
|
||||
>>> import math
|
||||
>>> math.PI
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
AttributeError: module 'math' has no attribute 'PI'
|
||||
>>> math.pi
|
||||
3.141592653589793
|
||||
>>>
|
||||
```
|
||||
|
||||
Now, when we call pi from the math module we got the result.
|
||||
|
||||
### KeyError
|
||||
|
||||
**Example 1: KeyError**
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> users = {'name':'Asab', 'age':250, 'country':'Finland'}
|
||||
>>> users['name']
|
||||
'Asab'
|
||||
>>> users['county']
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
KeyError: 'county'
|
||||
>>>
|
||||
```
|
||||
|
||||
As you can see, there was a typo in the key used to get the dictionary value. so, this is a key error and it is straight forward what to fix. Lets fix this.
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> user = {'name':'Asab', 'age':250, 'country':'Finland'}
|
||||
>>> user['name']
|
||||
'Asab'
|
||||
>>> user['county']
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
KeyError: 'county'
|
||||
>>> user['country']
|
||||
'Finland'
|
||||
>>>
|
||||
```
|
||||
|
||||
We debug our error and our code ran and we got the value.
|
||||
|
||||
### TypeError
|
||||
|
||||
**Example 1: TypeError**
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> 4 + '3'
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
TypeError: unsupported operand type(s) for +: 'int' and 'str'
|
||||
>>>
|
||||
```
|
||||
|
||||
In the above example, a TypeError is raised because we can not add number and string. First, we should convert the string to int or float. Let's fix this error.
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> 4 + '3'
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
TypeError: unsupported operand type(s) for +: 'int' and 'str'
|
||||
>>> 4 + int('3')
|
||||
7
|
||||
>>> 4 + float('3')
|
||||
7.0
|
||||
>>>
|
||||
```
|
||||
|
||||
The remove the error and our and we got the result we expected.
|
||||
|
||||
### ImportError
|
||||
|
||||
**Example 1: TypeError**
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> from math import power
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
ImportError: cannot import name 'power' from 'math'
|
||||
>>>
|
||||
```
|
||||
|
||||
There is no function called power in the math module instead we have _pow_. Lets correct it
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> from math import power
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
ImportError: cannot import name 'power' from 'math'
|
||||
>>> from math import pow
|
||||
>>> pow(2,3)
|
||||
8.0
|
||||
>>>
|
||||
```
|
||||
|
||||
### ValueError
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> int('12a')
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
ValueError: invalid literal for int() with base 10: '12a'
|
||||
>>>
|
||||
```
|
||||
|
||||
We can not change string to a number.
|
||||
|
||||
### ZeroDivisionError
|
||||
|
||||
```py
|
||||
Last login: Tue Dec 3 15:20:41 on ttys002
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> 1/0
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
ZeroDivisionError: division by zero
|
||||
>>>
|
||||
```
|
||||
|
||||
We can not divide a number by zero.
|
||||
|
||||
We have covered some of the python error types, if you want to check more about it check the python documentation about python error types.
|
||||
If you are good at reading the error types then you will be able to fix your bugs fast and you will be also a better programmer.
|
||||
|
||||
## 💻 Exercises: Day 15
|
||||
|
||||
1. Open you python interactive shell and try all the examples covered in this section.
|
||||
|
||||
[<< Part 4 ](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md) | [Part 6 >>](<[#](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme16-18.md)>)
|
||||
|
||||
|
||||
29
readme4-6.md
29
readme4-6.md
@ -1,5 +1,18 @@
|
||||
[Part 1](https://github.com/Asabeneh/30-Days-Of-Python) | [Part 2](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)| [Part 3](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)| [Part 4](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)| [Part 5](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
***
|
||||
|
||||

|
||||
|
||||
[Part 1: Day 1 - 3](https://github.com/Asabeneh/30-Days-Of-Python)
|
||||
[Part 2: Day 4 - 6](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)
|
||||
[Part 3: Day 7 - 9](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)
|
||||
[Part 4: Day 10 - 12](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)
|
||||
[Part 5: Day 13 - 15](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
[Part 6: Day 16 - 18](#)
|
||||
[Part 7: Day 19 - 21](#)
|
||||
[Part 8: Day 22 - 24](#)
|
||||
[Part 9: Day 25 - 27](#)
|
||||
[Part 10:Day 28 - 30](#)
|
||||
|
||||
---
|
||||
|
||||

|
||||
- [Day 4](#day-4)
|
||||
@ -18,7 +31,7 @@
|
||||
- [Reversing a string](#reversing-a-string)
|
||||
- [Skipping characters while slicing](#skipping-characters-while-slicing)
|
||||
- [String Methods](#string-methods)
|
||||
- [Exercises - Day 4](#exercises---day-4)
|
||||
- [💻 Exercises - Day 4](#%f0%9f%92%bb-exercises---day-4)
|
||||
- [Day 5](#day-5)
|
||||
- [Lists](#lists)
|
||||
- [How to create a list](#how-to-create-a-list)
|
||||
@ -40,7 +53,7 @@
|
||||
- [Finding index of an item](#finding-index-of-an-item)
|
||||
- [Reversing a list](#reversing-a-list)
|
||||
- [Sorting list items](#sorting-list-items)
|
||||
- [Exercises: Day 5](#exercises-day-5)
|
||||
- [💻 Exercises: Day 5](#%f0%9f%92%bb-exercises-day-5)
|
||||
- [Day 6:](#day-6)
|
||||
- [Tuple](#tuple)
|
||||
- [Creating Tuple](#creating-tuple)
|
||||
@ -51,7 +64,7 @@
|
||||
- [Checking an item in a list](#checking-an-item-in-a-list)
|
||||
- [Joining tuples](#joining-tuples)
|
||||
- [Deleting tuple](#deleting-tuple)
|
||||
- [Exercises: Day 6](#exercises-day-6)
|
||||
- [💻 Exercises: Day 6](#%f0%9f%92%bb-exercises-day-6)
|
||||
# Day 4
|
||||
## String
|
||||
Text is a string data type. Any data type written as text is a string. Any data under single or double quote are strings. There are different string methods and built-in functions to deal with string data types. To check the length of a string use the len() method.
|
||||
@ -434,7 +447,7 @@ challenge = '30 days of python'
|
||||
print(challenge.startswith('thirty')) # False
|
||||
```
|
||||
|
||||
## Exercises - Day 4
|
||||
## 💻 Exercises - Day 4
|
||||
1. Concatenate the string 'Thirty', 'Days', 'Of', 'Python' to a single string, 'Thirty Days Of Python'
|
||||
2. Concatenate the string 'Coding', 'For' , 'All' to a single string, 'Coding For All'
|
||||
3. Declare a variable name company and assign it to an initial value "Coding For All.
|
||||
@ -908,7 +921,7 @@ To sort list we can use *sort() method or *sorted()* builtin function. The sort(
|
||||
print(fruits) # ['orange', 'mango', 'lemon', 'banana']
|
||||
```
|
||||
|
||||
## Exercises: Day 5
|
||||
## 💻 Exercises: Day 5
|
||||
1. Declare an empty list
|
||||
2. Declare a list with more than 5 number of items
|
||||
3. Find the length of your list
|
||||
@ -1108,7 +1121,7 @@ It is not possible to remove a single item in a tuple but it is possible to dele
|
||||
```
|
||||
|
||||
|
||||
## Exercises: Day 6
|
||||
## 💻 Exercises: Day 6
|
||||
1. Create an empty tuple
|
||||
2. Create a tuple containing name of your sisters and your brothers
|
||||
3. Join brothers and sisters tuples and assign it to siblings
|
||||
|
||||
47
readme7-9.md
47
readme7-9.md
@ -1,8 +1,21 @@
|
||||
[Part 1](https://github.com/Asabeneh/30-Days-Of-Python) | [Part 2](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)| [Part 3](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)| [Part 4](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)| [Part 5](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
***
|
||||
|
||||

|
||||
- [Day 7](#day-7)
|
||||
|
||||
[Part 1: Day 1 - 3](https://github.com/Asabeneh/30-Days-Of-Python)
|
||||
[Part 2: Day 4 - 6](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme4-6.md)
|
||||
[Part 3: Day 7 - 9](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme7-9.md)
|
||||
[Part 4: Day 10 - 12](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme10-12.md)
|
||||
[Part 5: Day 13 - 15](https://github.com/Asabeneh/30-Days-Of-Python/blob/master/readme13-15.md)
|
||||
[Part 6: Day 16 - 18](#)
|
||||
[Part 7: Day 19 - 21](#)
|
||||
[Part 8: Day 22 - 24](#)
|
||||
[Part 9: Day 25 - 27](#)
|
||||
[Part 10:Day 28 - 30](#)
|
||||
|
||||
---
|
||||
|
||||

|
||||
- [📘 Day 7](#%f0%9f%93%98-day-7)
|
||||
- [Set](#set)
|
||||
- [Creating a set](#creating-a-set)
|
||||
- [Getting set length](#getting-set-length)
|
||||
@ -19,8 +32,8 @@
|
||||
- [Checking difference between two sets](#checking-difference-between-two-sets)
|
||||
- [Finding Symmetric difference between two sets](#finding-symmetric-difference-between-two-sets)
|
||||
- [Joining set](#joining-set)
|
||||
- [Exercises: Day 7](#exercises-day-7)
|
||||
- [Day 8](#day-8)
|
||||
- [💻 Exercises: Day 7](#%f0%9f%92%bb-exercises-day-7)
|
||||
- [📘 Day 8](#%f0%9f%93%98-day-8)
|
||||
- [Dictionary](#dictionary)
|
||||
- [Creating a dictionary](#creating-a-dictionary)
|
||||
- [Dictionary Length](#dictionary-length)
|
||||
@ -35,8 +48,8 @@
|
||||
- [Copy a dictionary](#copy-a-dictionary)
|
||||
- [Getting dictionary keys as list](#getting-dictionary-keys-as-list)
|
||||
- [Getting dictionary values as list](#getting-dictionary-values-as-list)
|
||||
- [Exercises: Day 8](#exercises-day-8)
|
||||
- [Day 9](#day-9)
|
||||
- [💻 Exercises: Day 8](#%f0%9f%92%bb-exercises-day-8)
|
||||
- [📘 Day 9](#%f0%9f%93%98-day-9)
|
||||
- [Conditionals](#conditionals)
|
||||
- [If condition](#if-condition)
|
||||
- [If Else](#if-else)
|
||||
@ -44,10 +57,10 @@
|
||||
- [Short Hand](#short-hand)
|
||||
- [Nested condition](#nested-condition)
|
||||
- [If condition and and logical operator](#if-condition-and-and-logical-operator)
|
||||
- [### If and or logical operator](#if-and-or-logical-operator)
|
||||
- [Exercises: Day 9](#exercises-day-9)
|
||||
- [If and or logical operator](#if-and-or-logical-operator)
|
||||
- [💻 Exercises: Day 9](#%f0%9f%92%bb-exercises-day-9)
|
||||
|
||||
# Day 7
|
||||
# 📘 Day 7
|
||||
## Set
|
||||
Let me take you back to your elementary or high school Mathematics lesson. The Mathematics definition of set can be applied also in python. Set is a collection of unordered and unindexed distinct elements. In python set uses to store unique items, and it is possible to find the *union*, *intersection*, *difference*, *symmetric difference*, *subset*, *super set* and *disjoint set* among sets.
|
||||
### Creating a set
|
||||
@ -281,7 +294,6 @@ st1 = {'item1', 'item2', 'item3', 'item4'}
|
||||
st2 = {'item2', 'item3'}
|
||||
# it mean (A\B)U(B)
|
||||
st2.symmetric_difference(st1) # {'item1', 'item4'}
|
||||
|
||||
```
|
||||
**Example:**
|
||||
```py
|
||||
@ -292,7 +304,6 @@ whole_numbers.symmetric_difference(even_numbers) # {0, 6, 7, 8, 9, 10}
|
||||
python = {'p', 'y', 't', 'o','n'}
|
||||
dragon = {'d', 'r', 'a', 'g', 'o','n'}
|
||||
python.symmetric_difference(dragon) # {'r', 't', 'p', 'y', 'g', 'a', 'd'}
|
||||
|
||||
```
|
||||
### Joining set
|
||||
If two set do not have common item or items we call it disjoint set. We can check if two sets are joint or disjoint using *isdisjoint()* method.
|
||||
@ -314,7 +325,7 @@ dragon = {'d', 'r', 'a', 'g', 'o','n'}
|
||||
python.disjoint(dragon) # False, there is common items {'o', 'n'}
|
||||
```
|
||||
|
||||
## Exercises: Day 7
|
||||
## 💻 Exercises: Day 7
|
||||
```py
|
||||
# sets
|
||||
it_companies = {'Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'}
|
||||
@ -338,7 +349,7 @@ age = [22, 19, 24, 25, 26, 24, 25, 24]
|
||||
14. Explain the difference among the following data types: string, list, tuple and set
|
||||
15. *I am a teacher and I love to inspire and teach people.* How many unique words have been used in the sentence.
|
||||
|
||||
# Day 8
|
||||
# 📘 Day 8
|
||||
## Dictionary
|
||||
A dictionary is a collection of unordered, modifiable(mutable) key value paired data type.
|
||||
### Creating a dictionary
|
||||
@ -582,7 +593,7 @@ dct = {'key1':'item1', 'key2':'item2', 'key3':'item3', 'key4':'item4'}
|
||||
values = dct.values()
|
||||
print(values) # dict_values(['item1', 'item2', 'item3', 'item4'])
|
||||
```
|
||||
## Exercises: Day 8
|
||||
## 💻 Exercises: Day 8
|
||||
1. Create a an empty dictionary called dog
|
||||
2. Add name, color, breed, legs, age to the dog, dictionary
|
||||
3. Create a student dictionary and add first_name, last_name, gender, age, marital status, skills, country, city and address as key for the dictionary
|
||||
@ -596,7 +607,7 @@ print(values) # dict_values(['item1', 'item2', 'item3', 'item4'])
|
||||
11. Delete the dictionary completely
|
||||
|
||||
|
||||
# Day 9
|
||||
# 📘 Day 9
|
||||
## Conditionals
|
||||
By default , statements in python script executed sequentially from top to bottom. If the processing logic require so, the sequential flow of execution can be altered in two way:
|
||||
* Conditional execution: a block of one or more statements will be executed if a certain expression is true
|
||||
@ -713,7 +724,7 @@ elif a == 0:
|
||||
else:
|
||||
print('A negative number')
|
||||
```
|
||||
### ### If and or logical operator
|
||||
### If and or logical operator
|
||||
```py
|
||||
# syntax
|
||||
if condition or condition:
|
||||
@ -730,7 +741,7 @@ else:
|
||||
print('A negative number')
|
||||
```
|
||||
|
||||
## Exercises: Day 9
|
||||
## 💻 Exercises: Day 9
|
||||
1. Get user input using input(“Enter your age:”). If user is 18 or older , give feedback:You are old enough to drive but if not 18 give feedback to wait for the years he supposed to wait for. Output:
|
||||
```sh
|
||||
Enter your age: 30
|
||||
|
||||
Loading…
Reference in New Issue
Block a user