Detailed Explanation of Variables and Simple Number Types of python

  • 2021-11-30 00:43:46
  • OfStack

Directory 1. Variable 1.1 Avoid Naming Errors When Using Variable Names 2. String 2.1 Method to Modify the Case of Strings 2.2 Merge Strings 2.3 Use Tab or Newline Characters to Add White Space 2.4 Remove White Space 2.5 Avoid Syntax Errors When Using Strings 3. Numeric Types 3.1 Integers 3.2 Floating Point Numbers 3.3 Complex Numbers 3.4 Use Function str () to Avoid Type Errors 4. Note 5. Zen Summary of python

1. Variables

Each variable stores 1 value Variables can be modified at any time in the program, but Python will always record the latest value of variables

message = "Hello Huang ZB!"
print(message)
message = "Goodbye Huang ZB!"
print(message)

1.1 Avoid naming errors when using variable names

Check Traceback to see the error


message = "Hello Huang ZB!"
print(mesage)

2. String

Def: A string is a string of characters. Both double quotation marks and single quotation marks can represent

2.1 How to modify the case of a string


name = "huang zhibin"
print(name.title())            #title() Function: Change the first letter of each word to uppercase 

Huang Zhibin

Other methods:


name = "huang zhibin"
print(name.title())   #title() Function: Change the first letter of each word to uppercase 
print(name.upper())   #upper() The string () function converts all string contents to uppercase 
print(name.lower())   #lower() The string () function converts all string contents to lowercase 

Huang Zhibin
HUANG ZHIBIN
huang zhibin

2.2 Merge Strings

Methods: Splicing


first_name = 'huang'
last_name = 'zhibin'
full_name = first_name + ' ' + last_name
print('Hello, ' + full_name.title() + '!')    # This  +  Indispensable 

Hello, Huang Zhibin!

2.3 Use tabs or line breaks to add white space

Add tabs to the string, using\ t (also understood as carry)

print("python")
print("\tpython")             # \t  Representing tabs 

python
python

Add a newline character to the string, using\ n


print("Languages:\nPython\nC\nJavaScript")       # \n  Represents a newline character 

Languages:
Python
C
JavaScript

The same 1 string can contain both tab and newline string "\ n\ t": Wrap python to the next 1 line


print("Languages:\n\tPython\n\tC\n\tJavaScript")

Languages:
Python
C
JavaScript

2.4 Remove Blanks

python can find excess white space at the beginning and end of a string. To ensure that there is no white space at the beginning and end, use rstrip () To ensure that there is no white space at the beginning, use the method lstrip () At the same time, eliminate the white space at both ends of the string, using the method strip ()

information = '     Life is too short, I learn python    '
print(information.rstrip())
print(information.lstrip())
print(information.strip())

Life is too short, I learn python

Life is too short, I learn python # The right space still exists!

Life is too short, I learn python

2.5 Avoid syntax errors when using strings

Syntax error is also an important check index when modifying the program again

3. Number type

3.1 Integer


>>> 2+3
5
>>> 5-6
-1
>>> 4*5
20
>>> 36/6
6.0
>>> 3**2
9
>>> 2+2**2
6
>>> (2+2)*2
8

3.2 Floating point numbers


message = "Hello Huang ZB!"
print(mesage)
0

Keep two decimal places


message = "Hello Huang ZB!"
print(mesage)
1

3.3 Complex


message = "Hello Huang ZB!"
print(mesage)
2

3.4 Use the function str () to avoid type errors


age = 21
message = "Happy " + str(age) + "rd Birthday!"     # Convert non-string values to strings 
print(message)

Happy 21rd Birthday!

4. Notes

Single-line comment

#

Multi-line comment

'''

Comments cannot be nested! ! ! ! ! In fact, in fact, the

5. Zen of python


message = "Hello Huang ZB!"
print(mesage)
4

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: