Code:[ZachxPKU/AcceleratedCPlusPlus]
Python Crash Course
Chapter 2 Variables and simple data types
2.1 What Really Happens When You Run hello_world.py
print("Hello Python world!")
2.2 Variables
message = "Hello Python world!"
print(message)
message = "Hello Python world!"
print(message)
message = "Hello Python Crash Course world!"
print(message)
2.2.1 Naming and Using Variables
- Variable names can contain only letters, numbers, and underscores. They can start with a letter or an underscore, but not with a number. For instance, you can call a variable message_1 but not 1_message.
- Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example, greeting_message works, but greeting message will cause errors.
- Avoid using Python keywords and function names as variable names; that is, do not use words that Python has reserved for a particular programmatic purpose, such as the word print.
- Variable names should be short but descriptive. For example, name is better than n, student_name is better than s_n, and name_length is better than length_of_persons_name.
- Be careful when using the lowercase letter l and the uppercase letter O because they could be confused with the numbers 1 and 0.
2.2.2 Avoiding Name Errors When Using Variables
message = "Hello Python Crash Course world!"
print(mesage)
A name error usually means we either forgot to set a variable’s value before using it, or
we made a spelling mistake when entering the variable’s name.
mesage = "Hello Python Crash Course world!"
print(mesage)
2.2.3 Variables Are Labels
TRY IT YOURSELF
- simple_message.py
- simple_messages.py
2.3 Strings
“This is a string.”
‘This is also a string.’
‘I told my friend, “Python is my favorite language!”’
“The language ‘Python’ is named after Monty Python, not the snake.”
“One of Python’s strengths is its diverse and supportive community.”
2.3.1 Changing Case in a String with Methods
name = "Ada lovelace"
print(name.title())
name = "Ada lovelace"
print(name.upper())
print(name.lower())
2.3.2 Using Variables in Strings
first_name = "zach"
last_name = "xiang"
full_name = f"{first_name} {last_name}"
print(full_name)
These strings are called f-strings.
first_name = "zach"
last_name = "xiang"
full_name = f"{first_name} {last_name}"
print(f"Hello, {full_name.title()}!")
```python
first_name = "zach"
last_name = "xiang"
full_name = f"{first_name} {last_name}"
message = f"Hello, {full_name.title()}!"
print(message)
F-strings were first introduced in Python 3.6. If you’re using Python 3.5 or earlier, you’ll need to use the format() method rather than this f syntax.
first_name = "zach"
last_name = "xiang"
full_name = "{} {}".format(first_name, last_name)
message = "Hello, {}!".format(full_name.title())
print(message)
2.3.3 Adding Whitespace to Strings with Tabs or Newlines
print("Python")
print("\tpython")
print("Languages:\nPython\nC\nJavaScript")
print("Languages:\n\tPython\n\tC\n\tJavaScript")
2.3.4 Stripping Whitespace
favorite_language = 'python '
favorite_language
favorite_language.rstrip()
favorite_language
favorite_language = 'python '
favorite_language = favorite_language.rstrip()
favorite_language
favorite_language = ' python '
favorite_language.lstrip()
favorite_language = ' python '
favorite_language.rstrip()
favorite_language = ' python '
favorite_language.strip()
In the real world, these stripping functions are used most often to clean up user input before it’s stored in a program.
2.3.5 Avoiding Syntax Errors with Strings
message = "One of Python's strengths is its diverse community."
print(message)
message = 'One of Python's strengths is its diverse community.'
print(message)
TRY IT YOURSELF
- personal_message.py
- name_cases.py
- famous_quote.py
- famous_quote2.py
- stripping_names.py
2.4 Numbers
2.4.1 Integers
2 + 3
3 - 2
2 * 3
3 / 2
3 ** 2
3 ** 3
10 ** 6
2 + 3 * 4
(2 + 3) * 4
2.4.2 Floats
0.1 + 0.1
0.2 + 0.2
2 * 0.1
2 * 0.2
0.2 + 0.1
3 * 0.1
2.4.3 Integers and Flolats
4 / 2
1 + 2.0
2 * 3.0
3.0 ** 2
2.4.4 Underscores in Numbers
universe_age = 14_000_000_000
print(universe_age)
This feature works for integers and floats, but it’s only available in Python 3.6 and later.
2.4.5 Multiple Assignment
x, y, z = 0, 0, 0
2.4.6 Constant
A constant is like a variable whose value stays the same throughout the life of a program. Python doesn’t have built-in constant types, but Python programmers use all capital letters to indicate a variable should be treated as a constant and never be changed:
MAX_CONNECTIONS = 5000
TRY IT YOURSELF
- number_eight.py
- favorite_number.py
2.5 Comments
2.5.1 How Do You Write Comments?
# Say hello to everyone.
print("Hello Python people!")
2.5.2 What Kind of Comments Should You Write?
It’s much easier to delete extra comments later on than it is to go back and write comments for a sparsely commented program.
TRY IT YOURSELF
- adding_comments
2.6 The Zen of Python
import this
python之禅
优美胜于丑陋(Python 以编写优美的代码为目标)
明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)
简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)
可读性很重要(优美的代码是可读的)
即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)
不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写 except:pass 风格的代码)
当存在多种可能,不要尝试去猜测
而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法)
虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido )
做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)
如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准)
命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)