Python Exercise for Beginners # 1 [Basic Data Types / If Statements]

This collection of questions was created for in-house planning and marketing staff. About 10 questions are asked each time.

If you have a problem you don't understand, you can find it by google with "necessary knowledge". I think that googled well is the best way to improve. It's difficult at first, but let's study tenaciously! !!

Target

  1. You will be able to perform four arithmetic operations
  2. Get used to working with different data types
  3. You will be able to make simple conditional branches with if statements.

About level

★ I use it very often. Let's be able to do it.

★★ It is used when performing a little complicated processing.

★★★ You are studying well. I think I can graduate from Python beginners.

★★★★ It's tricky, but it's often used in Python. Let's remember.

★★★★★ Oh. It seems that Python can be used for business as well! As expected.


Question contents

Python tutorial

  1. An Informal Introduction to Python https://docs.python.org/ja/3/tutorial/introduction.html 4.4. More Control Flow Tools https://docs.python.org/ja/3/tutorial/controlflow.html

--Basic data type - int - float - string - bool --list # I'll deal with it later --tuple # I will deal with it in detail later --dict # I'll deal with it later --set # I will deal with it in detail later --if statement


Question 1

Level ★ Required Knowledge Numerical Calculation / Interpreter Execution

With an interactive interpreter

1 + 2
3 - 4
2 * 5
8 / 2

Predict the result when you execute, and actually input and confirm.

Problem 2

Level ★ Required Knowledge Numerical Calculation / Interpreter Execution

In an interactive interpreter, find the quotient of 7 divided by 3 and less.
However, the quotient is an integer.

Problem 3

Level ★ Required knowledge Numerical calculation / execution from command line / storage in variables / output of numerical values

The following problems are otsukai.Create a py file and run it from the command line.

Cumulative number of users of a site is 50,There are 000 people.
3 this month,000 people visited the site.
What is the ratio of visitors to total users this month?

I want to use this script again next month, so let's store the numbers in variables.

Problem 4

Level ★ Required Knowledge String / Combine Strings / Output Strings

The following problems are aisatsu.Create a py file and run it from the command line.

You are creating a website.
The name is the user's name and the message is the greeting.
Store the sentence that combines the greeting and the name in the sentence variable and output it.

sample.py


name = 'taro'
message = 'Hello'

#In the sentence below'Hello taro'Please store it so that it becomes.
#Please output sentence when it can be stored

Problem 5

Level ★ Required knowledge String repetition

The following problem is cry.Create a py file and run it from the command line.

Concatenate 50 characters for "ku", 25 characters for "so", and 10 characters for "ぉ".

(Output example)Kukukuku....Squeaky shit.....So-so ぉ ぉ ぉ ぉ ぉ ぉ ぉ ぉ ぉ

Problem 6

Level ★ Required Knowledge Boolean / operator bond strength

Predict the following output result and execute it on the command line to check it.

2 < 2
2 <= 2
3 > 1
0 == 0
0 != 0
1 is 2
3 is not -3
'3' is 3
2020 > 2019 and 2019 > 2018
2020 > 2019 or 2021 < 2020
True or False or True and False

Problem 7

Level ★ Necessary knowledge Access to the array

For subsequent problems, create a file with an appropriate name and run it from the command line.

The user's name and age data is stored in an array.
Please output the data of "tadokoro-san" from this array.

sample.py


data = ['kobayashi 23', 'tanaka 53', 'tadokoro 24']

#Please output tadokoro's data from the data

Problem 8

Level ★ Required knowledge if statement

You are making a traffic light.
signal is'red'If'stop'To'yellow'If'caution!',
'blue'If'GOGO!'Please output.

sample.py


signal = 'red' #this is'yellow'May become'blue'May become

#Please use the if statement below to make a conditional branch.

Problem 9

Level ★★ Required knowledge: Manipulating strings / Slicing into strings / Length of strings

You want to add a descriptive text below the video thumbnail.
However, if the sentence is too long, it will not fit, so if it is larger than 20 characters, cut it with 19 characters.
 '...'I'm thinking of adding the process.

If it is 20 characters or less, output it as it is.

sample.py


sentence = 'This is a sample sentence. If it is 20 characters or more, it will be too long to fit, so it would be helpful if you could fit it in a nice way.'

#Divide the output according to the length of the sentence below

Problem 10

Level ★★★ Required Knowledge Array Length / Access to Array / Slice

The rank list includes names in descending order of sales performance this year.

Please extract the following data.
(1)The three best performers
(2)Odd-numbered people with good performance
(3)The three worst performers
(4)Person of achievement in the middle(However, when the total number is even n, n/Second person and n/2 -Please output the first person as well)

sample.py


rank = ['tanaka', 'sasaki', 'satou', 'simizu', 'koizumi', 'yoshioka', 'tamaru', 'kiyomiya']

#Below(1) - (4)Please output.
#Also,(4)For, even if you reduce one person from the rank list, check that the output is correct.

Answer / Explanation

Question 1

It should be noted that it is an operation between integer types. This time, the operation was divisible by 8/4 = 2, but what about something like 7/4 or 2.0 * 5? This area is a little complicated, but I think you should search by the keywords int type and float type.

Problem 2

You can get the quotient as an integer by using // as an operator instead of /. Also, you can get too much with %

Problem 3

Answer example

otsukai.py


visited_user = 3000
total_user = 50000

print(visited_user / total_user)

From command line tools (terminal or powershell)

$ python otsukai.py
#Or
$ python3 otsukai.py

If you execute, the result will be output.

development

Let's display The ratio of the number of visitors to the total number of users this month is 〇〇. If you get an error, you can solve it by reading the error code carefully.

Problem 4

Answer example

aisatsu.py



name = 'taro'
message = 'Hello'

print(name + ' ' + message)

development

It can also be output without using +. Use the format function built into string.

another.py


name = 'taro'
message = 'Hello'

print('{} {}'.format(name, message))
#Or
print(f'{name} {message}')

Problem 5

Answer example

cry.py


print('Ku' * 50 + 'So' * 25 + 'Mm' * 10)

Problem 6

Programming language operators have associative rules. You don't have to remember the rule itself, but remember the word "associative rule" so you can look it up. Also, Python provides comparison operators ʻis and not`, which can be used positively to improve readability.

Problem 7

sample_ans.py


data = ['kobayashi 23', 'tanaka 53', 'tadokoro 24']

print(data[2])
#Or
print(data[-1])

Keep in mind that you can specify from the end by specifying a negative number for the elements of the array.

Problem 8

Answer example

sample_ans.py


signal = 'red'

if signal == 'red':
    print('stop')
elif signal == 'yellow':
    print('caution!')
else:
    print('GOGO!')

Problem 9

Answer example

sample_ans.py


sentence = 'This is a sample sentence. If it is 20 characters or more, it will be too long to fit, so it would be helpful if you could fit it in a nice way.'

if len(sentence) > 20:
    print(sentence[:19] + '...')
else:
    print(sentence)

development

You can actually put it together

print(sentence[:19] + '...' if len(sentence) > 20 else sentence)

Problem 10

Answer example

sample_ans.py


rank = ['tanaka', 'sasaki', 'satou', 'simizu', 'koizumi', 'yoshioka', 'tamaru', 'kiyomiya']
n = len(rank)

#(1)
print(rank[:3])

# (2)
print(rank[::2])

# (3)
print(rank[::-1][:3]

# (4)
print([rank[n//2], rank[n//2 - 1]] if n % 2 == 0 else rank[n//2])

How was it? Output is important to improve your ability.

Even if you can't do it, let's work on it without giving up! Next time, I will deal with for statements and while statements based on the knowledge I have gained so far.

Recommended Posts

Python Exercise for Beginners # 1 [Basic Data Types / If Statements]
Python basics (variables, types, operators, if statements, for statements)
Basic Python grammar for beginners
Python Exercise for Beginners # 2 [for Statement / While Statement]
Basic story of inheritance in Python (for beginners)
python textbook for beginners
OpenCV for Python beginners
Python basic if statement
Let's analyze Covid-19 (Corona) data using Python [For beginners]
[For beginners] How to study Python3 data analysis exam
[For beginners] Learn basic Python grammar for free in 5 hours!
Python for Data Analysis Chapter 4
Learning flow for Python beginners
[For beginners] kaggle exercise (merucari)
Python3 environment construction (for beginners)
Python #function 2 for super beginners
Python for Data Analysis Chapter 2
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Python for Data Analysis Chapter 3
[Python] Introduction to graph creation using coronavirus data [For beginners]
Python for super beginners Python # dictionary type 1 for super beginners
Python course for data science_useful techniques
Python #index for super beginners, slices
[Pandas] I tried to analyze sales data with Python [For beginners]
[TouchDesigner] Tips for for statements using python
Preprocessing template for data analysis (Python)
Python #len function for super beginners
Data formatting for Python / color plots
Beginners use Python for web scraping (1)
Python application: data visualization part 1: basic
Run unittests in Python (for beginners)
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
[Super basic] Compare Python, Java and JavaScript (variable, if statement, while statement, for statement)
INSERT into MySQL with Python [For beginners]
[Python] Minutes of study meeting for beginners (7/15)
Python visualization tool for data analysis work
Summary of various for statements in Python
Let's put together Python for super beginners
[Python] Organizing how to use for statements
Time series data anomaly detection for beginners
[Python] Read images with OpenCV (for beginners)
How to use "deque" for Python data
WebApi creation with Python (CRUD creation) For beginners
Detailed Python techniques required for data shaping (2)
Atcoder standard input set for beginners (python)
[Python] I searched for various types! (Typing)
JupyterLab Basic Setting 2 (pip) for data analysis
[For beginners] Try web scraping with Python
JupyterLab Basic Setup for Data Analysis (pip)
A textbook for beginners made by Python beginners
Data analysis in Python Summary of sources to look at first for beginners
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
Basic data frame operations written by beginners in a week of learning Python
Memo # 4 for Python beginners to read "Detailed Python Grammar"
[Explanation for beginners] TensorFlow basic syntax and concept
The fastest way for beginners to master Python