Please refer to here for notes.
import random
x = random.randint(1, 3) #Substitute a random number from 1 to 3
if x == 1:
print("This is 1") #When the conditional expression is satisfied
elif x == 2:
print(“This is 2”) #Processing when conditional expression 2 is satisfied
else:
print("This is not 1 or 2") #When the conditional expression is not satisfied
・ Don't forget the: (colon) in the if statement An error will occur. (I often forget) ・ Not elsif It's easy to get confused because some languages use elsif.
Indenting the beginning of a line of code is called "indentation". In Python, enter one tab or four half-width spaces in the indent of one row.
#abridgement
if x == 2000:
print("2000 points")
In Python, it starts with a ":" (colon) after the conditional expression and a ":" (colon) after the else. Treat until the end of the indent line as a block.
Indentation, in Python, represents the process to be executed when a conditional expression is satisfied, and at the same time It makes it easier for programmers to distinguish the processing to be executed according to the conditional expression.
List
Example | meaning | Example to be true |
---|---|---|
a < b | a is less than b | 10 < 15 |
a > b | a is greater than b | 10 > 7 |
a <= b | a is less than or equal to b | 10 <= 15 |
a >= b | a is greater than or equal to b | 10 >= 7 |
a != b | a and b are not equal | 10 != 1 |
import random
age = random.randint(18, 22) #How old are 18 in age~Randomly assigned in the range of 22
text = ""
if age < 20:
text = "No drinking"
else:
text = "Drinkable"
print(str(age) + "Talent" + text)
Recommended Posts