python Programming (from introduction to practice) Chapter 1-2

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,

print("Hello Python world!")


Hello Python world!

Chapter 2 variables and simple data types


Try hello_ Add a variable to the program
message = "Hello Python world!"

message = "Hello Python Crash Course world!"


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
name = "ada lovelace"
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 = "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:

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.


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
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"

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()}!"


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")
\n newline
>>> print("Languages:\npython\nc\njava")
>>> print("Languages:\n\tpython\n\tc\n\tjava")
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

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
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


In programming, it is often used to record scores, represent visual data, store web application information, and so on. Let's take a look at how Python manages them.


In Python, you can add (+) subtract (-) multiply (*) divide (/) integers.

Python uses two multiplier signs to represent the power.

Directly return the operation result in the terminal session.

>>> 2 + 3
>>> 3 - 1
>>> 2 * 3
>>> 3 / 2
>>> 3 ** 2

python operations also support secondary operations, so you can use multiple operations in one expression.

>>> 2 + 3 * 4
>>> (2 + 3) * 4

In this operation, spaces do not affect the operation of the expression. They exist to enable you to quickly determine which operations to perform first when reading code.

Floating point number

Python calls all numbers with decimal points floating point numbers. To a large extent, using floating-point numbers doesn't need to consider their behavior. You just need to enter the numbers used, and python usually handles them as you want.

>>> 0.1 + 0.1
>>> 2 * 0.1

However, it should be noted that the decimal places contained in the results may be uncertain.

>>> 0.2 + 0.1
>>> 3 * 0.1

All languages have this problem, and Python will try to find a way to accurately represent the results. After that, we will learn how to deal with redundant decimals.

Operations on integers and floating point numbers:

  • When two numbers are divided, the result is always a floating-point number, even if the two numbers are integers and divisible;
  • In any other operation, if one operand is an integer and the other operand is a floating point number, the result is always a floating point number;
Underscores in numbers

When writing large numbers, underline can be used to group the numbers to make them clearer and easier to read;

>>> universe_age = 14_000_000_000
>>> print(universe_age)

When you print a number defined with underscores, python does not print the underscores. This representation is only supported in Python 3.6 and later.

Assign values to multiple variables at the same time

Assign values to variables x, y and z at the same time, separated by commas;

>>> x,y,z = 0,0,0
#The initial value is 0

Constants are similar to variables, but they remain unchanged throughout the life of a program. python has no built-in variable types, but it usually uses uppercase to indicate that variables should be treated as constants and their values should always remain unchanged.



In python, comments are usually identified by # numbers.


Tags: Python Linux

Posted on Tue, 30 Nov 2021 22:23:10 -0500 by mobilekid