1. Variables
- Each variable stores a value
- Variables can be modified at any time in the program, but Python will always record the latest value of the variable
message = "Hello Huang ZB!" print(message) message = "Goodbye Huang ZB!" print(message)
1.1 Avoid naming errors when using variable names
View Traceback understands the error
message = "Hello Huang ZB!" print(mesage)
2. Strings
Def: A string is a string of characters. Double quotes and single quotes can indicate that the
2.1 Methods to change string case
name = "huang zhibin" print(()) The #title() function does the following: capitalizes the first letter of each word.
Huang Zhibin
Other methods:
name = "huang zhibin" print(()) The #title() function does the following: capitalizes the first letter of each word. print(()) #upper () function function: the string content will be converted to all uppercase print(()) #lower() function role: the string content will be converted to all lowercase
Huang Zhibin
HUANG ZHIBIN
huang zhibin
2.2 Merging strings
Methods:put together
first_name = 'huang' last_name = 'zhibin' full_name = first_name + ' ' + last_name print('Hello, ' + full_name.title() + '!') # This # # Indispensable #
Hello, Huang Zhibin!
2.3 Using Tabs or Line Breaks to Add Whitespace
- To add tabs to a string, use \t (also understood as a rounding character)
print("python") print("\tpython") # \t means tab
python
python
To add a newline character to a string, use \n
print("Languages:\nPython\nC\nJavaScript") # \n Indicates a line break
Languages:
Python
C
JavaScript
It is possible to have both tabs and newlines in the same string String " \n\t ": make python go to the next line
print("Languages:\n\tPython\n\tC\n\tJavaScript")
Languages:
Python
C
JavaScript
2.4 Delete blanks
- Python is able to find extra whitespace at the beginning and end of a string, to ensure that there are no whitespaces at the end of the open end, use the following methodrstrip()
- To ensure that there are no gaps at the beginning of the opening, the method of uselstrip()
- Simultaneous elimination of whitespace at both ends of a string, usingstrip()
information = ' Life is short, I learn python ' print(()) print(()) print(())
Life is short, I'm learning python.
Life is short, I'm learning python # The right space is still there!
Life is short, I'm learning python.
2.5 The need to avoid syntax errors when using strings
Syntax errors are also an important indicator to check when re-modifying the program
3. Types of numbers
3.1 Integers
>>> 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
>>> 0.2+0.3 0.5 >>> 0.2-0.3 -0.09999999999999998
retain two decimal places
print ('{:.2}'.format(variant))
3.3 Complex numbers
>>> 2+6j (2+6j) >>> (2+6j).real 2.0 >>> (2+6j).imag 6.0
3.4 Using 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 . Commentary
single-line comment
#
multiline comment
‘''
Comments cannot be nested !!!!!
5 The Zen of .python
>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
summarize
That's all for this post, I hope it helped you and I hope you'll check back for more from me!