Learn Python from scratch

This article is participating in the Python learning direction of the Rising Star Project. For details, please see:https://bbs.csdn.net/topics/614101762


1. Some basic knowledge of Python

  1. variable

When you give a value a name, it will be stored in memory. This memory is called a variable, but in Python, this operation is more like "sticking" a label to the value. A variable is a name through which we can find what we want. It is called a variable because its value is changeable. When a second value is given to the same variable, it will change to the new value.

For example:

a = 4
a = 8
print(a)

At this time a changes from 4 to 8. Because there are no strict storage type requirements like C, variables in python can be values ​​of many types, so it is also possible for a = "Life is short, I use python" to be equal to such a string.

  • Before using a variable, you need to assign a value to it.

  • Variables can include letters, numbers, and underscores, but variable names cannot begin with numbers.

  • Letters can be uppercase or lowercase, but uppercase and lowercase are different. In other words, python and PYTHON are two different variable names.

  • The equal sign (=) means assignment, with the variable name on the left and the value on the right.

  • The variable name can be any legal name, but the name should be related to the program, and the camel case method can also be used.

  1. string

2.1 How to represent strings

We know that the content enclosed in quotes is a string. Strings are also called text. When using quotation marks to represent a string, they must be in pairs.

”Good“
'morning'

But sometimes what if we need to use quotation marks in the string? A common solution is to use the escape character (\) to escape the quotes in the string.

'I\'m Zhuerchong'

In this case, you can get a string like I'm Zhuerchong. Think about what would happen if there were \ without adding escape characters. Yes, something will go wrong, because the single quotes do not appear in pairs.

Another way is to take advantage of the properties of double quotes and single quotes.

"I'm Zhuerchong"
2.2 Original string

What if I need to print the symbol \? Yes, we can add the escape character \ to print,

string = 'I say \no'
print(string)

Why is this so? That's because \n means line break. Some escape characters are as follows:

escape character

illustrate

\n

Newline character, moves the cursor position to the beginning of the next line.

\r

Carriage return character moves the cursor position to the beginning of this line.

\t

The horizontal tab character, also known as the Tab key, is generally equivalent to four spaces.

\a

The buzzer sounds. Note that the sound is not from the speaker. Many computers nowadays do not have buzzers, so the ringing may not be effective.

\b

Backspace moves the cursor position to the previous column.

\\

backslash

\'

apostrophe

\"

Double quotes

\

The line continuation character at the end of a string line means that one line is not finished, so go to the next line and continue writing.

string = 'I say \\no'
print(string)

This works as we expected, but what if there are a lot of backslashes in a string? If I use backslashes to escape one by one, our code will become confusing and less readable. At this time, we need to use the original string, and just add the English letter r in front of the string. .

string = r'I say \no \no \no'
print(string)

Regardless of whether it is a raw string or not, it cannot end with a backslash, otherwise an error will be reported.

What if we insist on adding a \ at the end? We can do this

2.3 Long string

If I have a poem that needs to span multiple lines, this is how we currently handle it

You have to ask a big truck to load enough.

It never said a word. to 

The pressure on my back presses into my flesh,

It lowered its head heavily!    

I don’t know my fate at this moment,

It only swallows tears in its heart.     

A whip shadow floated in his eyes,

It raised its head and looked ahead.

print("总得叫大车装个够,\n它横竖不说一句话。 \n背上的压力往肉里扣,\n它把头沉重地垂下!\n这刻不知道下刻的命,\n它有泪只往心里咽\n眼里飘来一道鞭影,\n它抬起头望望前面。")

But if there are a lot of lines, it will be very troublesome to process. At this time we need to use long strings ("content").

print("""
总得叫大车装个够,
它横竖不说一句话。 
背上的压力往肉里扣,
它把头沉重地垂下!    
这刻不知道下刻的命,
它有泪只往心里咽。     
眼里飘来一道鞭影,
它抬起头望望前面。
""")
  1. type of data

3.1 Integer type

Integers are the integers we usually see. The length of integers in python3 is not limited, of course, unless it exceeds the total amount of virtual memory. Therefore, we can use python3 to perform large number operations.

3.2 Floating point type

Floating point types are the decimals we usually see. 4.33 is a decimal, and 7.2222 is also a decimal.

E notation is scientific notation, used to represent very large numbers and very small numbers. If you give a super decimal or super large number, python will use E notation:

Where e means exponent, which means the base is 10, and the number after e is the power of 10.

3.3 Boolean type

The Boolean type has only two cases: True and False. True means "true" and False means "false". The Boolean type can be treated as an integer, with True equivalent to the integer 1 and False equivalent to the integer 0.

  • However, it is inappropriate to use Boolean types as 1 and 0 to participate in operations, which can easily cause code confusion.

3.4 Type conversion

int(),float(),str()

The function of int() is to convert a string or floating point number into an integer

  • It should be noted that when a floating point number is converted to an integer, Python will only take the integer part and discard the decimal point instead of rounding.

The function of float() is to convert a string or integer into a floating point number

The function of str() is to convert a string or floating point number into a string

We can get the data type of a variable through the type() function

Guess you like

Origin blog.csdn.net/qq_73698300/article/details/129699190