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

number

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.

integer

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
5
>>> 3 - 1
2
>>> 2 * 3
6
>>> 3 / 2
1.5
>>> 3 ** 2
9

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

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

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
0.2
>>> 2 * 0.1
0.2

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

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

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

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
constant

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.

>>> MAX_CONECTIONS = 5000

notes

In python, comments are usually identified by # numbers.

Loading...

Tags: Python Linux

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