Continuing from Last time, I will continue to do Progate free lessons.
This time it will be Python.
Inexperienced. Interpreter language. Should I do it with a glue like JavaScript? w
Python I Official lesson
Progate had an installation procedure. → https://prog-8.com/docs/python-env-win
After installation, I would like to write it in VS Code and execute it. I was allowed to reference. → Prepare Python environment with VS Code
I will return to the lesson.
-Routine character string output In python
print
Seems to use.
print('Hello World')
・ Usual ... +-* /%
・ The usual one ...
String concatenation
'{}_{}_{}'.format(1,2,3) # 1_2_3
Is the mainstream?
****** [You pointed out in the comments. ] *** It seems that it can be written like this now.
a, b, c = 2020,3,20
print(f'today{a}Year{b}Month{c}It's a day')
VS Code results ** **
-Type conversion is required to concatenate characters and numbers. I think Ruby was the same ...? C # and java become characters without permission, though.
・ Character string with str (numerical value)
・ Numerical value with ʻint (character string) `
・ Hmm. I double the equality with ==
, but it is difficult to understand because there is no :
or {}
block. It may be difficult to understand if it becomes a little redundant or if there are many ifs.
・ As usual, omitted
・ In Python, ʻelif... It gets messed up with other languages.
else if
elseif
elsif
elif`
Wow ah ah ah ah ah ah ah I was allowed to reference. → other if of major web languages
****** [You pointed out in the comments. ] ***
ʻElse if is not a single conditional branch, it is just ʻelse
followed by ʻif, which is different from ʻelse if
and ʻelif. In Python, you can't write ʻif
in one line after ʻelse`, so it will be a line break.
if a == 2:
print('test')
else:
if a == 3:
print('test2')
I understand that ʻelif` is provided because the nesting becomes deep as described above.
If it were a specification that could write ʻelse ifin one line You need
{}or
then end`
I wonder if only one line can be written after the conditional branch.
(Python can be written multiple times if the indents are aligned)
** **
・ Too unique.
&&
Or||
I wanted you to ...
&&
→ and
||
→ or
!=
→ not ==
Hmm.
** Python has ʻand and ʻor
other uses **
message = "test1" and "test2" #test2 is assigned
message = "" and "test2" #""Is assigned
message = "test1" and "" #""Is assigned
message2 = "test3" or "test4" #test3 is assigned
message2 = "" or "test4" #test4 is assigned
message2 = "test3" or "" #test3 is assigned
Reason: -In the case of a character string, "" is interpreted as false, and the others are interpreted as positive.
・ ʻAnd` returns the result on the right when the expression on the left is positive → "test2" on the first expression, "" on the third expression When the left is false, the left is returned → The result of the second formula
・ ʻOr` returns the left when the expression on the left is positive → “test3” in the first expression, “test3” in the third expression When the left is false, the right is returned → "test4" in the second formula
JavaScript&&
Or||
It's the same as the operation of! Tabun
·nothing special
・ It seems that you can receive input with ʻinput. Is it
Console.Read ()` in C #?
** Exercise **
apple_price = 200
#Substitute the number 1000 for the variable money
money = 1000
input_count = input('Please enter the number of apples you want to buy:')
count = int(input_count)
total_price = apple_price * count
print('How many apples to buy' + str(count) + 'It is an individual')
print('The payment amount is' + str(total_price) + 'It's a yen')
#money and total_Please branch the condition according to the comparison result of price
if money > total_price:
print('Apples' + str(count) + 'I bought one')
print('The balance is' + (money - total_price) + 'It's a yen')
elif money == total_price:
print('Apples' + str(count) + 'I bought one')
print('The wallet is empty')
else:
print('Not enough money')
print('I couldn't buy an apple')
** Cleared **
・ It ended up being very short ... -It's very easy to program with Ruby, but the operators and conditional branching methods are too unique, so I personally prefer Ruby.
I'd like to try machine learning someday, so I'll try Python again at that time.
Next time, I would like to do command line. → Next time
Recommended Posts