Install the Python runtime environment
PyCharm install the Python interpreter Install pyCharm install pyCharmCopy the code
Prints Hello world
Interactive command line
- Terminal type python to enter the Python interactive command line.
- Next you can type python code inside: print(“Hello world”)
Terminal running script
- To write a Python script using VS Code, type the Python code: print(“Hello world”), save it as a test.py file, all Python files with the py suffix;
- The terminal executes the Python script and enters the Python file name.
Variables and comments
variable
Meaning of variables
- Extract variables, use the same variable in multiple places, if you want to modify the value of the variable only need to modify one place, convenient maintenance code;
- Variables can be dynamically changed, for example, there is a variable to store student information, that students may change names or new classmates, at this time, some methods of variables to meet the requirements of the program;
name="Zhang"
age=18
print(name) # zhang SAN
print(age) # 18
A more concise way to write multivariable assignment
name,age="Zhang".18
print(name) # zhang SAN
print(age) # 18
Copy the code
Variable naming rules
- Variable names can contain letters, numbers, and underscores, but cannot start with a number;
- Variable names cannot contain Spaces;
- Variable names cannot be the same as keywords.
The keyword
A change in the value of a variable
a = 1
a = "Zhang"
print(a) # zhang SAN
Copy the code
annotation
Single-line comments
# Comment content code will not be executed
Copy the code
Multiline comment
# single quote multiple line comments
First line comment second line comment and so on
# double quotes multi-line comment
""" First line comment second line comment and so on """
Copy the code
Common data types Look at the data type: type function
Numeric types
Integer int Numbers without a decimal point, such as -1, 2, 3…
Float A number with a decimal point, such as 1.0, -2.2…
Numerical calculations
Add +
Reduction –
* * by * * *
Divide by /, //, % /: divide to get decimal
/ / : take
% : modulo
* * to * * * *
string
# String with single quotes
Zhang SAN asked Li Si, "What's your name?" '
# String with double quotes
"Zhang SAN asked Li Si, 'What's your name? '"
String concatenation
name = "Zhang"
hobit = "Love football"
print(name + hobit) # Zhang SAN loves football
# String length
len(name) # 2
Copy the code
Number and string conversion
a = 2
b = "10"
d = "10.1"
Convert string d to float
e = float(d)
print(type(e)) # <class 'float'>
# int(b) converts string to int
print(a+int(b)) # 12
# STR (a) converts a number to a string
print(str(a)+b) # 210
Copy the code
Special characters
# \ n a newline
str = "Name \ N Zhang SAN"
print(str) # the nameZhang SAN# \ t indent
str = "Name \ T Zhang SAN"
print(str) # Name Zhang SAN
Copy the code
The index
# positive index 01234
str = "Hello"
Negative index -5-4-3-2-1
# take the last character: STR [-1]
print(str[-1]) # o
Copy the code
slice
str = "Hello world"
# [start position: end position: step size
str[0:4:2]
str3="Joe is playing ball."
# str3 index 0 through 3,4 not included, step 2
print(str3[0:4:2]) # in
# take the first to the fifth
# left included, right not included
print(str[0:5]) # Hello
# STR [start index: end index +1: step size] Step size is like climbing stairs. If it is 1, skip it and take one step at a time. If it is 2, take two steps at a time
print(str[: :2]) # Hlo
print(str[0:11:2]) # Hlo
# take o w
print(str[4:7:1]) # o w
print(str[4:7]) # o w
Copy the code
String method
String lookup
str = "hello world"
The # find method returns the index of the first element
print(str.find("hello")) # 0
If not found, return -1
print(str.find("111")) # 1
# find returns the index of the first element found
print(str.find("l")) # 2
# rfind returns the index of the last element found
print(str.rfind("l")) # 9
# index returns the index of the first element found
print(str.index("l")) # 2
# rindex returns the index of the last element found
print(str.rindex("l")) # 9
The # index method will throw an error if it is not checked
print(str.index("112")) # SyntaxError: invalid syntax
# Count the number of occurrences
print(str.count("l")) # 3
Copy the code
String substitution
str = "hello world"
# replace(the string to be replaced, the new string), returns a new string, not the original string
str1 = str.replace("l"."L")
print(str1) # heLLo worLd
Copy the code
String separation
str = "hello wor ld"
# find if there is a "" in the string, remove the whitespace, and place the cut element in the list
a = str.split("")
print(a) # ['hello', 'wor', 'ld']
names = "Zhang SAN; Li si. Fifty"
# check if there is a ";" in the string. ", found the ";" Remove and place the cut element in the list
nameList = names.split(";")
print(nameList) # # # # # # # #
# check the string to see if there is a 1 in it. If there is no 1, insert the whole string into the list
nameList1 = names.split("1")
print(nameList1) # # # # # # # # #
str = "hello world"
To find the first element to appear, take all elements to the left of the separator as the first element, the separator itself as the second element, and the element to the right of the separator as the third element
str1 = str.partition("o")
print(str1) # ('hell', 'o', ' wor ld')
LLL = LLL; LLL = LLL; LLL = LLL
str2 = str.partition("lll")
print(str2) # ('hello world', '', '')
Copy the code
The first letter of the string is capitalized
str = "hello world"
str1 = str.capitalize()
print(str1) # Hello world
Copy the code
Each word of the string is capitalized
str = "hello world"
str2 = str.title()
print(str2) # Hello World
Copy the code
Checks whether a string begins or ends with a string. Returns bool: True/False
str = "hello world"
print(str.startswith("hello")) # True
print(str.startswith("hello1")) # False
print(str.endswith("d")) # True
print(str.endswith("l")) # False
Copy the code
The string is uppercase
str = "hello world"
str1 = str.upper()
print(str1) # HELLO WORLD
str = "HELLO WORLD"
str1 = str.lower()
print(str1) # hello world
Copy the code
String removes certain characters, removes certain characters on both sides, does not remove the middle
str = "00000003210Runoob01230000000"
# Remove strings at the beginning and end
print(str.strip("0")) # 3210Runoob0123
# Remove the string on the left
print(str.lstrip("0")) # 3210Runoob01230000000
# Remove the string on the right
print(str.rstrip("0")) # 00000003210Runoob0123
The string in the middle of ### cannot be removed
print(str.strip("R")) # 00000003210Runoob01230000000
Copy the code
String type judgment
str = "helloworld1232"
If the string contains only numbers, return True. Otherwise, False is returned
print(str.isdigit()) # False
If the string contains only letters, return True. Otherwise, False is returned
print(str.isalpha()) # False
If there is at least one character in the string and all characters are letters or numbers, return True. Otherwise, False is returned
print(str.isalnum()) # True
spa = ""
If the string contains only Spaces, return True. Otherwise, False is returned
print(spa.isspace()) # True
Copy the code
String concatenation of elements in a list
# String concatenation of elements in the list
a = ["Zhang"."Bill"."Fifty"]
str = "he".join(a)
print(str) He is the king of five
Copy the code
Boolean expression
Print (1>2) # False print(a == 1 and b == 2) # True # Print (a == 2 or b == 2) # False #! Print (a == 2 or b == 2) # true #! Print (a! = 2) # TrueCopy the code
Input from the client is a string
num = input("Please enter a number:")
print(type(num)) # <class 'str'>
# convert string to int: int(num)
print(type(int(num))) # <class 'int'>
Copy the code