This article covers the relative basics

1. The comments

Comment mouse is not the execution of the comment is the explanation of the code, is to let people see a single line of comment shortcut/cancel single line comment CTL +, you can site more than one line, add a single line comment to it# comments are divided into unit comments and multi-line comments. Single-line comments start with a #
   print() a function in Pyhon that prints the contents of parentheses on the consoleprint('helloe word! ')
   # multiline comments. The contents of three quotes are multi-line comments
   """ "That's what's in those three quotes and that's multi-line comments and multi-line comments can be wrapped without executing """
   
Copy the code

2. Definition and use of variables

# Definition and use of variables
# define a variable name to store data love
name = 'love'
# Define a variable age to store data 18
age = 18
Print these variables using the function print
print(age)
print(name)
Variable name = new data value
Add 19 to the age variable
age = 19
print(age)
Copy the code

3. Data types


The data type of a variable is determined by the data stored in the variable
We can use the type () function to get the data type of the variable. To print, we need to use the print function

# int integer
result = 10
Use the type () function to obtain the data type 2 of result. Use the print function to print this data type
print(type(result)) #<class 'int'>

# floot decimal
result = 3.14  # Data modification
print(type(result)) #<class 'float'>

# STR quotes produce a string containing both single and double quotes
name = 'isac'
print(name)
print(type(name))  #<class 'str'>
name = 'hello'
print(type(name))

The Boolean type has only two values True and False
Write booleans with capital letters
result = True
print(type(result))  #<class 'bool'>
Copy the code

4. Identifiers and keywords

1.Identifier naming rules: == Contains letters, digits, and underscores (_) and cannot start with a digit ==2.Naming conventions for variables2.1Follow the rules for identifiers2.2See of knowledge meaning2.3Use underscores2.4System keywords cannot be usedCopy the code

5. Output and input

The output in Python uses the print function
# Basic output
print("hello")  # will print Hello
print(123)  # will print 123

Output multiple contents at once
print('isaac'.18)  # prints Isaac and 18. They are separated by a space

# You can write expressions
print(1 + 2)  # will print the result 3 of 1 +2

Format output. Formatting placeholders (pits), %s string, %d int Integer int %f decimal float
name = 'isaac'
My name is XXX and I am very happy
print('My name is %s and I am very happy. ' % name)
age = 18
My age is 18
print('My age is %d'. % age)

height = 180.5
# %f prints decimals. Six decimal places are reserved by default
print('My height %f cm' % height)
My height is 180.50 cm
print('My height %.2f cm' % height)

My name is XX and MY age is XX. Height is XXCM
print('My name is %s, my age is % D, and my height is % FCM' % (name, age, height))

When using formatted output, to print a %, you need to use two %%
print('%d%%' % 50)

# Support for f-string since version 3.6. Use {} as placeholders, and fill data directly into {}
print(F prime My name is{name}Is the age,{age}He is two years old and his height is{height}cm')

The # escape character \n combines \ and n as one character, \n for newline
The # print() function adds a newline by default, which can be removed if you don't want it
# print('hello',end='')
print('hello', end='_ * _')
print('world')
print('good good study \n day day up')

Input: Takes input from the keyboard and stores it in a computer program
# Input () and functions are used in Python
password = input('Please enter your password')
print('The password you entered is %s' % password)

Copy the code

6. Data type conversion

# 1. The price of apple after using the inout function
# price = input(' input apple price ') # STR
# 2. Use the input function to get the weight of the purchase
# weight = input(' Please enter weight ') # STR

# 3. Output the desired result
# result = float(price) * float(weight) # convert the type to a decimal
# print({result} ${price} ${weight} ${result} $) # print({result} ${price} ${weight} ${result} $)

# Type conversion, convert the original data to the data type we need. In this process, the original data will not be changed, but a new data will be generated
Type int (raw data)
# 1.1 Convert data of type float to int
pi = 3.14
num = int(3.14)
print(type(pi))  # a float
print(type(num))  # type int

String of integer type. "10"
my_str = '10'
num1 = int(my_str)
print(type(my_str))  # str
print(type(num1))   # int

# 2. Convert to float type float ()
# 2.1 int -----> float
num2 = 10
num3 = float(num2)
print(type(num2))  # int
print(type(num3))  # float

# 2.2 Convert numeric string to float "10" "3.14"
num4 = float("3.14")
num5 = float('10')
print(type(num4))  # float
print(type(num5))   # float

# eval() restores the original data type, removing the string quotes
num6 = eval('100')
num7 = eval('3.14')
print(type(num6))  # int
print(type(num7))   # float

Copy the code

7. Operator

Arithmetic operator

"" "+ - * / / / divisible (commercial) % modulo * * index, power operation () can change the priority ", ""
Copy the code

The assignment operator

""" = assign the result on the right-hand side of the equal sign to the variable on the left-hand side of the equal sign, it must be a variable, not a specific number ""
Copy the code

Conforms to the assignment operator

""" xxxxxxxxxx += c+=a ===> c = c + a """
Copy the code

Comparison operator

== determine if it is equal, and equal isTrue.unequal yesFalse! = Determine if it is unequal, unequal yesTrueEqual,False> < > = < =Copy the code

Logical operator

Logical operators can concatenate expressions, and the result of the two expressions together determines whether the final result is True or False

andThe two conditions for the logical connection must beTrue, the results forTrueOne false is false if the first condition isFalseThe second condition will not be judgedorBoth conditions of logic or connection are zeroFalse, the results forFalseTrue is true if the first condition isTrueThe second condition will not be judgednotLogical non, take the inverse, it isTruetoFalseTurns out to beFalsetoTrue
Copy the code

Operator case

# 1. Read age input () through user keyboard input
age = input('Please enter your age')
We need to convert a string age to a non-int age
age = int(age)
I am 18 years old. I am 18 years old now. You can go into an Internet cafe and do whatever you want.
if age >= 18:
    print('I'm 18. I can go into an Internet cafe and do whatever I want.')
    # execute only if conditions are met
else:
    print('Under 18, go back and study boy!! ')

# 3. Program final output. If judgment end
print('If judgment ends')

Copy the code

Operator Case (2)

# demand
# 1. The score is greater than or equal to 90 and the output is excellent
# 2. Score greater than or equal to 80, less than 90, good output
# 3. A score greater than or equal to 60, less than 80, pass the output
# 4. Output below 60 fails

score = eval(input('Please enter your score'))
# 1. The score is greater than or equal to 90 and the output is excellent
if score >= 90:
    print('good')
# 2. Score greater than or equal to 80, less than 90, good output
elif(score >= 80) and score < 90:
    print('good')
# 3. A score greater than or equal to 60, less than 80, pass the output
elif(score >= 60) and score < 80:
    print('pass')
else:
    print('Fail')

print('End of program')
Copy the code