Chapter 1 setting up programming environment
Installation environment
All Python exercises in this article are applicable to the python 3.6 environment. If the execution fails, you can use the python 3.6 environment.
A simple python program, hello_world.py
#cat hello_world.py print("Hello Python world!")
Run hello_world.py
#python hello_world.py Hello Python world!
Chapter 2 variables and simple data types
variable
Try hello_ Add a variable to the python.py program
#cat hello_world.py message = "Hello Python world!" print(message) message = "Hello Python Crash Course world!" print(message)
function
#python hello_world.py Hello Python world! Hello Python Crash Course world!
Note the naming rules of variables: they can only contain letters, numbers and underscores, and cannot start with spaces and numbers.
Pay attention to the use specification
character string
Use the method to modify the size of the string
#cat name.py name = "ada lovelace" print(name.title())
function
#python name.py Ada Lovelace
The method title() displays each word in uppercase. For example, you might want to think of ADA, ADA, and ADA as the same name ADA.
How to handle case
# cat name.py name = "ada lovelace" print(name.upper()) print(name.lower())
implement
#python name.py ADA LOVELACE ada lovelace
The method lower() is useful when storing data. Many times, you can't rely on the user to provide the correct case, so you need to convert strings to lowercase first and then store them. When you need to use them later, convert them to appropriate case.
Using variables in strings
You may want to make two variables represent first and last names, and then combine the two values to display names:
#cat full_name.py first_name = "ada" last_name = "lovelace" full_name = f"{first_name} {last_name}" #To insert the value of a variable into a string, you can add the letter f(format) before the quotation marks. This string is called an f string. print(full_name)
implement
#python3 full_name.py ada lovelace
Using strings can accomplish many tasks, use the information associated with variables to create a complete message and create a well formed greeting message
#cat full_name.py first_name = "ada" last_name = "lovelace" full_name = f"{first_name} {last_name}" print(f"Hello,{full_name.title()}!") You can also use f String to create a message, and then assign the whole message to a variable first_name = "ada" last_name = "lovelace" full_name = f"{first_name} {last_name}" message = f"Hello,{full_name.title()}!" print(message)
implement
#python3 full_name.py Hello,Ada Lovelace!
Use tabs or line breaks to add white space
\t tab character, skip to the next "tab position", tab key, 8 bits.
>>> print("\tpython") python
\n newline
>>> print("Languages:\npython\nc\njava") Languages: python c java >>>
>>> print("Languages:\n\tpython\n\tc\n\tjava") Languages: python c java
Delete blank
Whitespace is important because it is often necessary to compare whether two strings are the same. An important example is to check the user name when a user logs in to the website.
python can find extra white space in "python" and still make sense for it, unless you tell it it's not.
>>> favorite_language='python ' >>> favorite_language 'python ' #Print variables and find spaces >>> favorite_language.rstrip() #Call method rstrip() 'python' #The extra space has been deleted >>> favorite_language #View again 'python ' #It is found that the space appears again. It was just deleted temporarily
To permanently delete this space, you must associate the delete operation with a variable.
>>> favorite_language = 'python ' >>> favorite_language = favorite_language.rstrip() #Associate rstrip() directly to the variable >>> favorite_language 'python'
You can also eliminate the whitespace at the beginning of the string, or both sides of the string at the same time. To do this, use the methods lstrip() and strip(), respectively:
In practical programs, these culling functions are often used to clean up user input before storing it.
>>> favorite_language = ' python ' >>> favorite_language = favorite_language.lstrip() >>> favorite_language 'python ' >>> favorite_language = favorite_language.strip() >>> favorite_language 'python'
Avoid syntax errors when using strings
How to use single quotation marks and double quotation marks correctly
message = "One python's strengths is its community"
The apostrophe is between two double quotes, so the python interpreter can understand the string correctly
message = "One python's strengths is its community." >>> print(message) One python's strengths is its community.
However, if you use single quotes, python cannot determine the end position of the string.
>>> message = 'One python's strengths is its community.' File "<stdin>", line 1 message = 'One python's strengths is its community.' ^ SyntaxError: invalid syntax
Loading