Python 1:
Python course syllabus:
Class 1: Intro+Variable
Class 2: Condition+Loop execution
Class 3: Data type- string, integer, list, tuple
Class 4: Function&class+ Data type-dictionary
Class 5: reading and writing files
一. Python environment
Jupyter notebook(本地)
Colab: Google cloud based(云端,需要google account)
二. Variable
1. Constants: fixed values such as numbers, letters, and strings are called "constants"(常量),because their value does not change.
Numeric constants are as you expect; String constants(一般指字母) use single quotes(') or double quotes(")(数字不加引号,字母可加单引号或双引号)
2. Variables: a variable is a named place in the memory where a programmer can store data and later retrieve the data using the variable "name".
Programmers get to choose the names of the variables. You can change the contents of a variable in a later statement.
3. Python Variable Name Rules:
Must start with a letter or underscore_,如:spam egg spam23 _speed, 如果这样写23spam #sign var.12,python读不出来(variable里不能有点,#或空格等字符)
Must consist of letters and numbers and underscores
Case Sensitive:大小写不一样,记录的variable也不一样
4. Reserved Words(python内设词): you cannot use reserved words as variable names/identifiers(python系统内部已设置好的variable,是无法复制的,但如果把首字母改成大写,也是可以在这类词附加变量值,但一般不这么操作,以免出现confusion),如:and,del,for,is,raise,assert,elif,from,lambda,return,break,else,global,not,try,class,except,if,or,while,continue,exec,import,pass,yield,def,finally,in,print,as,with
5. Sentences or Lines展示
x = 2(assign 2给 x)
x = x+2 或 x += 2(replace x的值等于原x+2 )
print x (return x的结果)
x是variable,=和+是operator,2是constant,print是reserved word
6. Numeric Expressions
because of the lack of mathematical symbols on computer keyboards, we use "computer-speak" to express the classic math operations, 如:**代表power(指数,2 ** 3 = 8),%代表 remainder(余数)
7. Operator Precedence Rules
Highest precedence rule to lowest precedence rule(由高到低):
Parenthesis are always respected
Exponentiation
Multiplication, DIvision, and Remainder
Addition and Subtraction
Left to right
8. Type: in Python variables, literals and constants have a "type". Python knows the difference between an integer number and a string.(data type必须相同,才能做相关的operation, 可以数字+数字,字符+字符,不能数字+字符或字符+数字)
9. Several Types of Numbers: two main types
Integers are whole numbers, 如:-14,-2,0,1,100,401233
Floating Pointing Numbers have decimal parts, 如:-2.5,0.0,98.6,14.0
There are other number types- they are variations on float and integer
10. Type Conversions:
Integer conversions-When you put an integer and floating point in an expression, the integer is implicitly converted to a float, you can control this with the built-in functions int() and float().
String conversions-You can also use int() and float() to convert between strings and integers. You will get an error if the string does not contain numeric characters.(数字可以通过int()和float()把包含数字的string转换成数字,或把数字转换成string,但不能把不含任何数字的string转换成数字)
11. User Input: We can instruct Python to pause and read data from the user using the raw_input() function. The raw_input() function returns a string.
12. Comments in Python:
Anything after a # is ignored by Python.
Why comment- describe what is going to happen in a sequence of code; document who wrote the code or other ancillary information; turn off a line of code perhaps temporarily
13. String Operations
some operators apply to strings, 如 + implies “concatenation”,print 'abc' + '123' returns abc123;* implies "multiple concatenation", print 'Hi' * 5 returns HiHiHiHiHi
Python knows when it is dealing with a string or a number and behaves appropriately