You will be an engineer in 100 days --Day 31 --Python --Python Exercise 2

Today is a Python language exercise.

Click here until yesterday

You will be an engineer in 100 days-Day 30-Python-Basics of Python language 6

Basic exercise 2

I'm sorry if it doesn't appear

Let's solve the problem while reviewing. !! !!

What I've done so far ... Control statements, comprehensions, various built-in functions, About data types such as lists.

If you don't know, take a look at the videos of the lectures so far.

Basic Exercise 1:

Let's declare a list type variable (3 or more elements are optional)

Basic Exercise 2:

Has a string-type key and a numeric value Let's create a dictionary type variable (3 or more elements are optional).

Basic Exercise 3:

Let's create a dictionary type variable and add aelement (key: value)

Basic Exercise 4:

ʻIF --Let's create a process using the ELSE statement`

The condition is that the value of the integer type variable ʻa` For even numbers: Print "even numbers" For odd numbers: Print "odd"

Basic Exercise 5:

Using the ʻenumerate and rangefunctions in theforstatement Let's print the two values. The argument of therange function is 10`.

Basic Exercise 6:

Put three integer values in list type variable and output the maximum value.

Basic Exercise 7:

Next sentence " Urawa, Minami Urawa, Kitaurawa, Higashiurawa, Nishiurawa, Musashiurawa, Nakaurawa, Urawa "

I want to divide this by and and store it as a key in a dictionary type variable. The value is the empty string ``.

Basic Exercise 8:

I want to create a list type that has10elements of waste. Can't you write well using include notation?

Basic Exercise 9:

The multiplication table is the result of multiplying the numbers from 1 to 9. Let's display this using the for statement. In addition, let's display it as a character and align the digit.

Basic Exercise 10:

Question 9, is it possible to write this well in 1 line with inclusion notation? I think there are various ways to write it, so let's try it.

If you don't get an answer right away, stop the video and think about it.

The trick is what to enter, how to calculate and how to output Let's write while thinking about it.

The answer is below

Answer

Basic Exercise 1: Answer

Let's declare a list type variable (3 or more elements are optional)

#The list is[]Enclose and define
a = [1,2,3,4,5]

Basic Exercise 2: Answer

Has a string-type key and a numeric value Let's create a dictionary type variable (3 or more elements are optional).

#The dictionary type is{}Defined in wave brackets
d = {'a':1 , 'b':2 , 'c':3}

Basic Exercise 3: Answer

Let's create a dictionary type variable and add aelement (key: value)

d = {'a':1 , 'b':2 , 'c':3}
#Addition of dictionary elements is variable name[Key] =value
d['d'] = 4
print(d)

{'c': 3, 'd': 4, 'a': 1, 'b': 2}

Basic Exercise 4: Answer

ʻIF --Let's create a process using the ELSE` statement

The condition is that the value of the integer type variable ʻa` For even numbers: Print "even numbers" For odd numbers: Print "odd"

a = 10
if a%2==0:
    print('Even')
else:
    print('Odd')

Even

Basic Exercise 5: Answer

Using the ʻenumerate and rangefunctions in theforstatement Let's print the two values. The argument of therange` function is 10.

#Store the result of enumerate in one variable.
for i in enumerate(range(10)):
    print(i)

(0, 0) (1, 1) (2, 2) (3, 3) (4, 4) (5, 5) (6, 6) (7, 7) (8, 8) (9, 9)

#Store the result of enumerate in two variables.
for i,r in enumerate(range(10)):
    print(i , r)

0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

Basic Exercise 6: Answer

Put three integer values in list type variable and output the maximum value.

a = [10,20,30]
print(max(a))

30

Basic Exercise 7: Answer

Next sentence " Urawa, Minami Urawa, Kitaurawa, Higashiurawa, Nishiurawa, Musashiurawa, Nakaurawa, Urawa "

I want to divide this by and and store it as a key in a dictionary type variable. The value is the empty string ``.

a = 'Urawa, Minami Urawa, Kitaurawa, Higashiurawa, Nishiurawa, Musashiurawa, Nakaurawa, Urawa'
#Use the split function to separate with.
b = a.split('、')
#Make it a dictionary type with inclusion notation
d = {k:'' for k in b}
print(d)

{'Musashi Urawa':'','Nakaurawa':'','Higashi Urawa':'','Urawa':'','Minami Urawa':'','Kitaurawa':'','West Urawa':''}

a = 'Urawa, Minami Urawa, Kitaurawa, Higashiurawa, Nishiurawa, Musashiurawa, Nakaurawa, Urawa'
#Use the split function to separate with.
b = a.split('、')
#When not using comprehension
d = {}
for k in b:
    d[k]=''
print(d)

{'Musashi Urawa':'','Nakaurawa':'','Higashi Urawa':'','Urawa':'','Minami Urawa':'','Kitaurawa':'','West Urawa':''}

Basic Exercise 8: Answer

I want to create a list type that has 10 elements of waste. Can't you write well using include notation?

muda = ['Useless' for i in range(10)]
print(muda)

['Waste','Waste','Waste','Waste','Waste','Waste','Waste','Waste','Waste','Waste']

Basic Exercise 9: Answer

The multiplication table is the result of multiplying the numbers from 1 to 9. Let's display this using the for statement. In addition, let's display it as a character and align the digit.

for i in range(1,10):
    for j in range(1,10):
        #Format to 2-digit notation,Eliminate line feed code
        print('{:02}\t'.format(i*j), end='')
    print() #Only line breaks

01 02 03 04 05 06 07 08 09 02 04 06 08 10 12 14 16 18 03 06 09 12 15 18 21 24 27 04 08 12 16 20 24 28 32 36 05 10 15 20 25 30 35 40 45 06 12 18 24 30 36 42 48 54 07 14 21 28 35 42 49 56 63 08 16 24 32 40 48 56 64 72 09 18 27 36 45 54 63 72 81

Basic Exercise 10: Answer

Question 9, is it possible to write this well in 1 line with inclusion notation? I think there are various ways to write it, so let's try it.

#A list with 9 character strings for one column,Use the join function again to join the list to characters with line breaks
print('\n'.join([''.join(['{:02}\t'.format(i*j) for i in range(1,10) ])for j in range(1,10)]))

01 02 03 04 05 06 07 08 09 02 04 06 08 10 12 14 16 18 03 06 09 12 15 18 21 24 27 04 08 12 16 20 24 28 32 36 05 10 15 20 25 30 35 40 45 06 12 18 24 30 36 42 48 54 07 14 21 28 35 42 49 56 63 08 16 24 32 40 48 56 64 72 09 18 27 36 45 54 63 72 81

Summary

How was the basic exercise? By writing code while following each process one by one I think we will be able to solve the problem.

I don't think you will use multiplication tables for work, etc. To learn to build a program It is the best subject.

I think there are other ways to write than the solution introduced above. Please try various solutions.

If you don't understand, you can use string, format times, repeated times, etc. Please refer to it and deepen your understanding. I think it will gradually be solved.

69 days until you become an engineer

Author information

Otsu py's HP: http://www.otupy.net/

Youtube: https://www.youtube.com/channel/UCaT7xpeq8n1G_HcJKKSOXMw

Twitter: https://twitter.com/otupython

Recommended Posts

You will be an engineer in 100 days --Day 27 --Python --Python Exercise 1
You will be an engineer in 100 days --Day 34 --Python --Python Exercise 3
You will be an engineer in 100 days --Day 31 --Python --Python Exercise 2
You will be an engineer in 100 days ――Day 24 ―― Python ―― Basics of Python language 1
You will be an engineer in 100 days ――Day 30 ―― Python ―― Basics of Python language 6
You will be an engineer in 100 days ――Day 25 ―― Python ―― Basics of Python language 2
You will be an engineer in 100 days --Day 63 --Programming --Probability 1
You will be an engineer in 100 days --Day 65 --Programming --Probability 3
You will be an engineer in 100 days --Day 64 --Programming --Probability 2
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
You will be an engineer in 100 days --Day 35 --Python --What you can do with Python
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
You will be an engineer in 100 days --Day 86 --Database --About Hadoop
You will be an engineer in 100 days ――Day 71 ――Programming ――About scraping 2
You will be an engineer in 100 days ――Day 74 ――Programming ――About scraping 5
You will be an engineer in 100 days ――Day 73 ――Programming ――About scraping 4
You will be an engineer in 100 days ――Day 75 ――Programming ――About scraping 6
You will be an engineer in 100 days --Day 68 --Programming --About TF-IDF
You will be an engineer in 100 days ――Day 70 ――Programming ――About scraping
You will be an engineer in 100 days ――Day 81 ――Programming ――About machine learning 6
You will be an engineer in 100 days ――Day 82 ――Programming ――About machine learning 7
You will be an engineer in 100 days ――Day 79 ――Programming ――About machine learning 4
You will be an engineer in 100 days ――Day 76 ――Programming ――About machine learning
You will be an engineer in 100 days ――Day 80 ――Programming ――About machine learning 5
You will be an engineer in 100 days ――Day 78 ――Programming ――About machine learning 3
You will be an engineer in 100 days ――Day 84 ――Programming ――About machine learning 9
You will be an engineer in 100 days ――Day 83 ――Programming ――About machine learning 8
You will be an engineer in 100 days ――Day 77 ――Programming ――About machine learning 2
You will be an engineer in 100 days ――Day 85 ――Programming ――About machine learning 10
You will be an engineer in 100 days ――Day 60 ――Programming ――About data structure and sorting algorithm
You become an engineer in 100 days ――Day 67 ――Programming ――About morphological analysis
When you get an error in python scraping (requests)
Quicksort an array in Python 3
Will the day come when Python can have an except expression?
Write an HTTP / 2 server in Python
Until you put Python in Docker
Develop an investment algorithm in Python 2
Python in is also an operator
An alternative to `pause` in Python
Become an AI engineer soon! Comprehensive learning of Python / AI / machine learning / deep learning / statistical analysis in a few days!
[Python3] Code that can be used when you want to cut out an image in a specific size
Tkinter could not be imported in Python
If you write the View decorator in urls.py in Django, the list will be higher.
If an exception occurs in the function, it will be transmitted to the caller 2
If an exception occurs in the function, it will be transmitted to the caller 1