Today is the end of the basic Python exercises.
Click here for the last time [You will be an engineer in 100 days --Day 33 --Python --Basics of Python language 8] (https://qiita.com/otupy/items/9e70a3b36f32fccacadf)
Let's practice based on what we have learned so far.
It's made a little difficult, so Please think slowly and solve while stopping the video.
Let's create a program that finds the sum
from the integers 1
to 100
.
Like Fibonacci number
(0,1,1,2,3,5,8,13 ...)
The first two terms are 0
, 1
, and every subsequent term is the sum of the two terms immediately preceding it It is called the
Fibonacci number` of the number that becomes.
Let's create a function
that finds this Fibonacci number
.
Let's make it a function
that displays up to the Fibonacci number
of 3 digits
.
Using the functions of the random
library dealt with in the previous section
I used only lowercase letters a to z or the numbers 0
to 9
Let's create a program that creates a 32 digit string
.
random.randint (minimum, maximum)
Can return a random integer value.
aabacdcda
Let's aggregate
bycharacter
of this string.
Keiko's husband
, Chidori
, Mentalist
Let's create a function
that returns one of these three strings
at random
.
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 create a program that finds the sum
from the integers 1
to 100
.
res = 0
# range(1,101)From 1 to 100
for i in range(1,101):
# +=Add with
res += i
print(res)
5050
#If you write the above in a comprehension
print(sum([i for i in range(1,101)]))
5050
Like Fibonacci number
(0,1,1,2,3,5,8,13 ...)
The first two terms are 0
, 1
, and every subsequent term is the sum of the two terms immediately preceding it It is called the
Fibonacci number` of the number that becomes.
Let's create a function
that finds this Fibonacci number
.
Let's make it a function
that displays up to the Fibonacci number
of 3 digits
.
def fib():
#First of all, prepare two variables
a = b = 1
while True:
print(b)
#The Fibonacci number is the sum of the previous two terms
a, b = b, a+b
#Exit if it exceeds 3 digits
if b>999:
break
#Execution of the above function
fib()
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
Using the functions of the random
library dealt with in the previous section
I used only lowercase letters a to z or the numbers 0
to 9
Let's create a program that creates a 32 digit string
.
random.randint (minimum, maximum)
Can return a random integer value.
import random
#Prepare 36 alphanumeric characters
words = 'abcdefghijklmnopqrstuvwxyz0123456789'
#Create an array by repeating the random return from the above 32 times.
#Join the array with join and convert it to a string
print(''.join([words[random.randint(0,35)] for i in range(32)]))
lfkj6bv913np7cq8fxzjjpfjv2u8qv0q
aabacdcda
Let's aggregate
bycharacter
of this string.
#First, prepare the characters to be aggregated
word = 'aabacdcda'
#Prepare a dictionary to store the results
result_dict = {}
for w in word:
#If there are letters+If not 1, store in the dictionary with 1.
if w in result_dict:
result_dict[w]+=1
else:
result_dict[w]=1
print(result_dict)
{'b': 1, 'c': 2, 'a': 4, 'd': 2}
Keiko's husband
, Chidori
, Mentalist
Let's create a function
that returns one of these three strings
at random
.
import random
#Prepare an array
daigo = ['Keiko's husband','Chidori','mentalist']
#Create a function
def random_daigo(daigo):
#Randomly returns an integer value and returns the elements of the above array at the index.
return daigo[random.randint(0,2)]
#Function execution
print(random_daigo(daigo))
Chidori
Randomly returns one element directly to the random
library
There is a function called choice
.
random.choice (array)
import random
daigo = ['Keiko's husband','Chidori','mentalist']
def random_daigo(daigo):
return random.choice(daigo)
print(random_daigo(daigo))
Chidori
How was the exercise?
Has your wish
been achieved?
When you can program
You may be able to fulfill that wish
.
Programming
First of all, imitating and writing code copying sutras
is the key to improvement.
Let's write, write, write.
If you couldn't do it, please review the exercises.
This is the end of the basic Python exercises
Now that you have the basics, you have learned most of the grammar. To proceed with coding the Python language itself I think there are no obstacles.
Now let's write the code ourselves. Writing code is the most important thing in programming.
Let's decide the purpose and make some simple program at first.
For the frequently used code, I made a summary as a cheat sheet.
I will post a link here, so please refer to it. https://note.com/otupy/n/n1bedb9f36e54
66 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