Basic knowledge of Python. This is my study memo. Please do not excessive expectations.
-The extension of the Python program file is py
例:××××.py
・ Shell window IDLE started
・ Editor window
-The one that inputs the program is called a text editor
-Create new file-> " File "->" New File "
in the menu bar of IDLE's shell window
-Save-> When writing a program in the editor window and saving the file, " File "->" Save as ... "
in the menu bar of the editor window.
-Open File-> " File "->" Open "
in the menu bar of the editor window
-Run program-> "Run"-> "Run Module F5" in the menu bar of the editor window `
-Output print ()
Use the command print ()
to output a character string
regardless of whether it is full-width or half-width.
Also, to handle character strings, use double quotation ("")
andsingle quotation ('')
to wrap around.
Example: print ("Good morning") → Good morning
・ Input ʻinput () If you use it like
variable = input ('the string you want to display on the console')`, the value entered in the console will be assigned to the variable.
Also, since the value received by input becomes a character string type, it is necessary to convert it to a numeric type (use "int").
・ How to name variables
-1, Use English words, the first letter of the variable name cannot be a number
Example: date
-2, When using variable names of two or more words, separate words with _ (underscore)
Example: user_name
-Retrieve the value of a variable
name = "Jhon"
print(name) # → Jhon
print("name") # → name #If you add double quotation marks, it will be recognized as a character string.
-Update the value of the variable
Uninflected word | Labor-saving type |
---|---|
X = X + 100 | X += 100 |
X = X - 100 | X -= 100 |
X = X * 100 | X *= 100 |
X = X / 100 | X /= 100 |
X = X % 100 | X %= 100 |
-Use " str "
to convert a numeric type to a string type
Example
price = 200 #Numeric type
print("The price of apples"+ str(price) + "It's a yen")
-Use " int "
to convert string type to numeric type
Example
count = "3"
price = 100
total_price = price * int(count)
How to write
××××.py
score = 50
if score ==100: #Colon required at end of line
print("Equal numbers")
#Align the indents (4 half-width spaces)
××××.py
score = 50
if score ==100: #Colon required at end of line
print("Equal numbers")
#Align the indents (4 half-width spaces)
else: #Colon required at end of line
print("Different numbers")
#Align the indents (4 half-width spaces)
××××.py
score = 80
if score ==100: #Colon required at end of line
print("Equal numbers")
#Align the indents (4 half-width spaces)
elif score >=70: #Colon required at end of line
print("Greater than 70")
else: #Colon required at end of line
print("Different numbers")
#Align the indents (4 half-width spaces)
××××.py
# and -"When both condition 1 and condition 2 are satisfied"
x = 20
if x>=10 and x<=30:
print("x is 10 or more and 30 or less")
# or -"When condition 1 or condition 2 is satisfied"
y = 60
if y<10 or y>30:
print("y is less than 10 or greater than 30")
# not -"If the conditional expression is False, it will be True"
z = 55
if not z== 77:
print("z is not 77")
formula | meaning |
---|---|
== | When the right side and the left side are equal |
!= | When the right side and the left side are not equal |
< | When the time on the right side is large |
<= | When the time on the right side is large or equal |
> | When the time on the left side is large |
>= | When the time on the left side is large or equal |
Recommended Posts