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
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.
Let's declare a list type variable
(3 or more elements are optional)
Has a string-type key
and a numeric value
Let's create a dictionary type variable
(3 or more elements are optional).
Let's create a dictionary type variable
and add aelement (key: value)
ʻ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"
Using the ʻenumerate and
rangefunctions in the
forstatement Let's print the two values. The argument of the
range function is
10`.
Put three integer values
in list type variable
and output the maximum value.
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 ``.
I want to create a list type
that has10
elements of waste
.
Can't you write well using include notation
?
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
.
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
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]
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}
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}
ʻ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
Using the ʻenumerate and
rangefunctions in the
forstatement Let's print the two values. The argument of the
range` 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
Put three integer values in list type variable
and output the maximum value.
a = [10,20,30]
print(max(a))
30
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':''}
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']
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
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
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
Otsu py's HP: http://www.otupy.net/
Youtube: https://www.youtube.com/channel/UCaT7xpeq8n1G_HcJKKSOXMw
Twitter: https://twitter.com/otupython
Recommended Posts