You will be an engineer in 100 days --Day 34 --Python --Python Exercise 3

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)

Basic exercise 3

I'm sorry if it doesn't appear

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.

First question:

Let's create a program that finds the sum from the integers 1 to 100.

Second question:

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 theFibonacci 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.

Third question:

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.

Question 4:

aabacdcda Let's aggregate bycharacterof this string.

Question 5:

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

Answer

First question: Answer

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

Second question: Answer

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 theFibonacci 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

Third question:

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

Question 4: Answer

aabacdcda Let's aggregate bycharacterof 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}

Question 5: Answer

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.

Summary

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

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 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 61 ――Programming ――About exploration
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
You become an engineer in 100 days ――Day 66 ――Programming ――About natural language processing
When you get an error in python scraping (requests)
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!
Tkinter could not be imported in Python
If you add sudo on ubuntu, it will be called the default 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