Python Course by Google
Naming rules and conventions
命名規則與慣例
When assigning names to objects, programmers adhere to a set of rules and conventions which help to standardize code and make it more accessible to everyone. Here are some naming rules and conventions that you should know:
-
Names cannot contain spaces.
-
Names may be a mixture of upper and lower case characters.
-
Names can’t start with a number but may contain numbers after the first character.
-
Variable names and function names should be written in snake_case, which means that all letters are lowercase and words are separated using an underscore.
-
Descriptive names are better than cryptic abbreviations because they help other programmers (and you) read and interpret your code. For example, student_name is better than sn. It may feel excessive when you write it, but when you return to your code you’ll find it much easier to understand.
Annotating variables by type
This has several benefits: It reduces the chance of common mistakes, helps in documenting your code for others to reuse, and allows integrated development software (IDEs) and other tools to give you better feedback.
How to annotate a variable:
a = 3 #a is an integer
captain = "Picard" # type: str
captain: str = “Picard”
import typing
# Define a variable of type str
z: str = "Hello, world!"
# Define a variable of type int
x: int = 10
# Define a variable of type float
y: float = 1.23
# Define a variable of type list
list_of_numbers: typing.List[int] = [1, 2, 3]
# Define a variable of type tuple
tuple_of_numbers: typing.Tuple[int, int, int] = (1, 2, 3)
# Define a variable of type dict
dictionary: typing.Dict[str, int] = {"key1": 1, "key2": 2}
# Define a variable of type set
set_of_numbers: typing.Set[int] = {1, 2, 3}
Data type conversions
Implicit vs explicit conversion 隱式 vs 顯式轉換
Implicit conversion is where the interpreter helps us out and automatically converts one data type into another, without having to explicitly tell it to do so.
Example:
# Converting integer into a float
print(7+8.5)
Explicit conversion is where we manually convert from one data type to another by calling the relevant function for the data type we want to convert to.
We used this in our video example when we wanted to print a number alongside some text. Before we could do that, we needed to call the str() function to convert the number into a string.
- str() - converts a value (often numeric) to a string data type
- int() - converts a value (usually a float) to an integer data type
- float() - converts a value (usually an integer) to a float data type
Example:
# Convert a number into a string
base = 6
height = 3
area = (base*height)/2
print("The area of the triangle is: " + str(area))
Operators
Arithmetic operators
//
整數除法%
餘數除法**
次方
Good coding style
-
Create a reusable function - Replace duplicate code with one reusable function to make the code easier to read and repurpose.
-
Refactor code - Update code so that it is self-documenting and the intent of the code is clear.
-
Add comments - Adding comments is part of creating self-documenting code. Using comments allows you to leave notes to yourself and/or other programmers to make the purpose of the code clear. 加入註解是建立自我文件化程式碼的一部分。使用註解可讓您為自己和/或其他程式設計師留下紀錄,以清楚說明程式碼的目的
Resources
Naming rules and conventions
Annotating variables by type