All is the essence, no nonsense, let you learn python quickly!!!
1.
print('hello world!') print("Hello Python interpreter!")
2.
2.1
print("Hello Python world!")
message = "Hello Python world!" print(message)
2.3 string
#A string is a series of characters. In Python, strings are enclosed in quotation marks, which can be single quotation marks,
#It can also be double quotation marks
c = "This is a string." print(c)
2.3.1
name = "ada lovelace" print(name.title())
#title() displays each word in uppercase, that is, the first letter of each word is changed to uppercase #To change the string to all uppercase upper() #Or all lowercase lower()
name = "Ada Lovelace" print(name.upper()) print(name.lower())
2.3.2 merge (splice) strings
Splicing:
first_name = "ada" last_name = "lovelace" full_name = first_name + " " + last_name print(full_name) print("Hello, " + full_name.title() + "!") message = "Hello, " + full_name.title() + "!" print(message)
2.3.3 use tabs or line breaks to add white space
#To add tabs to a string, you can use character combinations \ t
print('\tpython')
#Adds a newline character to a string, using character combinations \ n
print('Language:\nC\nJava\nPython')
#Can be combined \ t\n
2.3.4 delete blank
#Temporarily delete rstrip()
favorite_language = "python " print(favorite_language.rstrip())
#Make sure there is no blank space at the end of the string. You can use the method rstrip()
#Permanent deletion operation: store the deleted result in the original variable again
#You can also eliminate whitespace at the beginning of the string (lstrip()), or both ends of the string (strip()). For this purpose, methods can be used separately
#lstrip() and strip():
2.3.5 avoid syntax errors when using strings
message = "One of Python's strengths is its diverse community." print(message)
#Right #Wrong
# message = 'One of Python's strengths is its diverse community.' # print(message)
2.4 figures
2.4.1 integer
#In Python, you can add (+) subtract (-) multiply (*) divide (/) integers.
#Python uses two multiplier signs to represent a power operation
2.4.2 floating point number
#The number of decimal places included in the result may be uncertain
# 0.2+0.1
# 0.30000000000000004
#2.4.3 using the function str() to avoid type errors
age = 23
message = "Happy " + str(age) + "rd Birthday!" # Read 23 as a whole with str()
print(message)
2.5 notes
#Say hello to everyone
print("Hello Python people!")
3. Introduction to the list
3.1 what is the list
#A list consists of a series of elements arranged in a specific order. Square brackets ([]) are used to represent the list, and commas are used to separate the elements
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles)
3.1.1 accessing list elements
#Lists are ordered collections, so to access any element of a list, you just tell Python the location or index of that element.
#To access a list element, indicate the name of the list, then the index of the element, and place it in square brackets.
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[0])
#When you request a list element, Python returns only that element, not square brackets and quotation marks:
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[0].title())
#Can be used in combination with methods
3.1.2 index starts from 0 instead of 1
#Python provides a special syntax for accessing the last list element. By specifying an index of - 1, python returns
#Back to the last list element
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[-1])
3.1.3 use the values in the list
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] message = "My first bicycle was a " + bicycles[0].title() + "." print(message)
3.2 modifying, adding and deleting elements
3.2.1 modifying list elements
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) motorcycles[0] = 'ducati' # List name plus modified location print(motorcycles)
#You can change the value of any list element, not just the value of the first element.
# 1. Add an element at the end of the list append()
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) motorcycles.append('ducati') print(motorcycles)
#The method append() adds the element 'ducati' to the end of the list without affecting all other elements in the list: # 2. Insert element in list #Use the insert() method to add a new element anywhere in the list.
motorcycles = ['honda', 'yamaha', 'suzuki'] motorcycles.insert(0, 'ducati') print(motorcycles)
3.2.3 removing elements from the list
#(1) Delete elements using del statements
#If you know the position of the element to be deleted in the list, you can use the del statement.
#Use del to delete a list element anywhere, provided you know its index.
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) del motorcycles[0] print(motorcycles)
#(2) Delete the element using the method pop() #Sometimes you want to delete an element from the list and then use its value #The method pop() deletes the element at the end of the list and allows you to use it later
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) popped_motorcycle = motorcycles.pop() print(motorcycles) print(popped_motorcycle)
#(3) Pop up the element anywhere in the list. You can use pop() to delete the element anywhere in the list. Just specify the index of the element to be deleted in parentheses
motorcycles = ['honda', 'yamaha', 'suzuki'] first_owned = motorcycles.pop(0) print('The first motorcycle I owned was a ' + first_owned.title() + '.')
#Whenever you use pop(), the pop-up element is no longer in the list #If you want to delete an element from the list and no longer use it in any way, use the del statement; If you want to continue after deleting the element #To use it, use the method pop(). #(4) Delete element based on value #If you only know the value of the element to be deleted, you can use the method remove() #When you use remove() to remove an element from the list, you can then use its value #The method remove() deletes only the first specified value. If the value to be deleted may appear more than once in the list, you need to #Loop to determine whether all such values have been deleted
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] print(motorcycles) motorcycles.remove('ducati') print(motorcycles) motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] print(motorcycles) too_expensive = 'ducati' motorcycles.remove(too_expensive) print(motorcycles) print("\nA " + too_expensive.title() + " is too expensive for me.")
#The value 'ducati' has been removed from the list, but it is also stored in the variable too_ In expense
3.3 organization list
3.3.1 permanently sort the list using the {method sort()
Pay attention to the difference between methods and functions
#The sort() method permanently modifies the order of the list elements. Now, cars are arranged in alphabetical order
#It can no longer be restored to the original order
cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() print(cars) # ['audi', 'bmw', 'subaru', 'toyota']
#You can also arrange the list elements in reverse alphabetical order by simply passing parameters to the sort() method # reverse=True. The following example arranges the list of cars in reverse alphabetical order:
cars.sort(reverse=True) print(cars) # ['toyota', 'subaru', 'bmw', 'audi']
3.3.2 use the {sorted() function to temporarily sort the list
#The function sorted() allows you to display list elements in a specific order without affecting their original order in the list.
cars = ['bmw', 'audi', 'toyota', 'subaru'] print("Here is the original list:") print(cars) # Here is the original list: # ['bmw', 'audi', 'toyota', 'subaru'] print("\nHere is the sorted list:") print(sorted(cars)) # Here is the sorted list: # ['audi', 'bmw', 'subaru', 'toyota'] print("\nHere is the original list again:") print(cars) # Here is the original list again: # ['bmw', 'audi', 'toyota', 'subaru']
#If you want to display the list in reverse alphabetical order, you can also pass the parameter reverse=True to the function sorted().
3.3.3 print list upside down
#Note that reverse() does not mean that the list elements are arranged in the reverse alphabetical order, but just reverse the order of the list elements:
#The method reverse() permanently modifies the order of the list elements, but it can be restored to the original order at any time. To do this, just call reverse() on the list again
cars = ['bmw', 'audi', 'toyota', 'subaru'] print(cars) cars.reverse() print(cars) # ['bmw', 'audi', 'toyota', 'subaru'] # ['subaru', 'toyota', 'audi', 'bmw']
3.3.4 determine the length of the list
#Use the function len() to quickly learn the length of the list
cars = ['bmw', 'audi', 'toyota', 'subaru'] len(cars)