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! !!
★ 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.
Python tutorial
--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
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.
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.
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.
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
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 ぉ ぉ ぉ ぉ ぉ ぉ ぉ ぉ ぉ
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
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
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.
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
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.
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.
You can get the quotient as an integer by using //
as an operator instead of /
. Also, you can get too much with %
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.
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.
Answer example
aisatsu.py
name = 'taro'
message = 'Hello'
print(name + ' ' + message)
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}')
Answer example
cry.py
print('Ku' * 50 + 'So' * 25 + 'Mm' * 10)
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.
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.
Answer example
sample_ans.py
signal = 'red'
if signal == 'red':
print('stop')
elif signal == 'yellow':
print('caution!')
else:
print('GOGO!')
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)
You can actually put it together
print(sentence[:19] + '...' if len(sentence) > 20 else sentence)
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