Python- PCEP entry level

Module1

1.1.1.3 absolute basics

  1. Source code : a program written in a high-level programming language is called source code
  2. File containing the source code is called the source file.

1.1.1.4 Compilation vs interpretation

  1. Compilation
    1. 编译:通过获取包含机器代码的文件(例如,如果代码打算在MS Windows下运行,则为.exe文件),将源程序翻译一次(但是,每次修改源代码时必须重复此操作);现在您可以在全球范围内分发该文件;执行这种翻译的程序称为编译器或翻译器;
  2. Interpretation
    1. 解释-您(或代码的任何用户)可以在每次运行源程序时对其进行翻译;执行这种转换的程序称为解释器,因为它每次执行代码时都会解释代码;这也意味着不能按原样分发源代码,因为最终用户还需要解释器来执行它。

1.1.1.6 Compilation vs. interpretation

Python是个interpreted language:

Due to historical reasons, languages designed to be utilized in the interpretation manner are often called scripting languages, while the source programs encoded using them are called scripts.

1.1.2 Python

And while you may know the python as a large snake, the name of the Python programming language comes from an old BBC television comedy sketch series called Monty Python's Flying Circus.

Python的发明者是: Guido van Rossum

Python的目的:  easy and intuitive; open source; understandable; suitable for everyday tasks

1.1.3

Cpython:原版的Python

Cython:比原版的Python更加有效率;自动转化Python代码到C代码

Jpython:一个用java写出来的python

Pypy and Rpython:一个用Rpython(restricted Python)写出来的Python:PyPy的源代码不是用interpretation manner的方式来run的,而是会translated到C语言然后再进行执行;

1.2.1 Begin your Python Journey

IDLE: integrated Development and Learning Environment

Module2

2.1.1  Hello World

  1. Function的来源分三类:
    1. Python自带的:Built in
    2. 来源于Module:需要额外的安装程序
    3. 自己写的:
  2. Print函数:
    1. 输出string的时候,需要双引号
    2. 可以输出String, numbers, characters, logical value, objects等
    3. Print() 中间不包含任何东西的时候,是会输出一个空行出来
    4. \n可以实现换行的功能  A new line character -- to start a new output line;
    5. \有escape Nature,如果想output一个\,需要print("\\")
    6. "\"本身没有意义,它是告诉系统它后面跟着的是存在一个特别的含义的。并不是它后面跟着所有字母都会有特殊的含义的
    7. print()括号中要输出的东西可以用逗号来隔开;但是隔开的字符串之间自动的生成空格;逗号隔开的会全部输出在同一行
    8. Print()函数中的“end”参数的默认设置是end="\n"; 所以如果在print()中把end=""的话,那么就不会和下一个print之间有默认的换行了。
    9. Print("","",sep="-"),会用破折号链接多个""之间;包括所连接的最后,也会有一个-
  3. 函数中参数的类型:
    1. Keyword argument:参数是根据keyword来识别的,而不是根据其所在的位置;keyword argument一定要在positional argument的后面出现;
    2. Positional argument
  4. Key takeaways:
    1. The print() function is a built-in function. It prints/outputs a specified message to the screen/consol window.
    2. Built-in functions, contrary to user-defined functions, are always available and don't have to be imported. Python 3.8 comes with 69 built-in functions. You can find their full list provided in alphabetical order in the Python Standard Library.
    3. To call a function (this process is known as function invocation or function call), you need to use the function name followed by parentheses. You can pass arguments into a function by placing them inside the parentheses. You must separate arguments with a comma, e.g., print("Hello,", "world!"). An "empty" print() function outputs an empty line to the screen.
    4. Python strings are delimited with quotes, e.g., "I am a string" (double quotes), or 'I am a string, too' (single quotes).
    5. Computer programs are collections of instructions. An instruction is a command to perform a specific task when executed, e.g., to print a certain message to the screen.
    6. In Python strings the backslash (\) is a special character which announces that the next character has a different meaning, e.g., \n (the newline character) starts a new output line.
    7. Positional arguments are the ones whose meaning is dictated by their position, e.g., the second argument is outputted after the first, the third is outputted after the second, etc.
    8. Keyword arguments are the ones whose meaning is not dictated by their location, but by a special word (keyword) used to identify them.
    9. The end and sep parameters can be used for formatting the output of the print() function. The sep parameter specifies the separator between the outputted arguments (e.g., print("H", "E", "L", "L", "O", sep="-"), whereas the end parameter specifies what to print at the end of the print statement.

2.2.1 Python Literals

  1. Print(Oo123) 八进制的123
  2. Print(Ox123)十六进制的123
  3. 300000000可以写作3E8,是可以认的 3可以是integer也可以不是,但是8必须是个integer;
  4. 6.62607E-34 注意,如果输入的是0.00000000000001,可能Python输出的也会是1E-15的形式,因为Python会选择最经济快捷的方式进行输出;
  5. 4是integer 但是4.0是floating-point
  6. String
    1. 如果想打印出来双引号“”怎么办?Print('I like "Monty python"')即可,也可以用escape character: \"
    2. String可以是空的,可以不包含任何characters在内;
  7. Boolean Values: True False
  8. None: 代表了absence of a value
  9. Key takeaways
    1. Literals are notations for representing some fixed values in code. Python has various types of literals - for example, a literal can be a number (numeric literals, e.g., 123), or a string (string literals, e.g., "I am a literal.").
    2. The binary system is a system of numbers that employs 2 as the base. Therefore, a binary number is made up of 0s and 1s only, e.g., 1010 is 10 in decimal.
    3. Octal and hexadecimal numeration systems, similarly, employ 8 and 16 as their bases respectively. The hexadecimal system uses the decimal numbers and six extra letters.

Integers (or simply ints) are one of the numerical types supported by Python. They are numbers written without a fractional component, e.g., 256, or -1 (negative integers).

  1. Floating-point numbers (or simply floats) are another one of the numerical types supported by Python. They are numbers that contain (or are able to contain) a fractional component, e.g., 1.27.
  2. To encode an apostrophe or a quote inside a string you can either use the escape character, e.g., 'I\'m happy.', or open and close the string using an opposite set of symbols to the ones you wish to encode, e.g., "I'm happy." to encode an apostrophe, and 'He said "Python", not "typhoon"' to encode a (double) quote.
  3. Boolean values are the two constant objects True and False used to represent truth values (in numeric contexts 1 is True, while 0 is False.

2.3.1 Operator

  1. ** 次方  如果底数和次方两个参数中有一个是float,那么计算出来的结果也是float
  2. *  乘  如果底数和次方两个参数中有一个是float,那么计算出来的结果也是float
  3. / 除 除的结果一定是个float数
  4. // 整除:
    1. 向下取整数 goes to the lesser integer--- floor division :近似到一个最接近且比实际结果小的整数身上 
    2. 如果底数和次方两个参数中有一个是float,那么计算出来的结果也是float
    3. Print(6//4)     1
    4. Print(6.0//4)   1.0
    5. Print(-6//4) -2
    6. Print(-6.//4) -2.0
  5. % 取除后的余数
  6. 大部分都是从左到右进行乘除的;但是**次方,是从右到左的: 2**2**3的结果是 256
  7. List of priorities

  1. Key takeaways:
    1. An expression is a combination of values (or variables, operators, calls to functions ‒ you will learn about them soon) which evaluates to a certain value, e.g., 1 + 2.
    2. Operators are special symbols or keywords which are able to operate on the values and perform (mathematical) operations, e.g., the * operator multiplies two values: x * y.
    3. Arithmetic operators in Python: + (addition), - (subtraction), * (multiplication), / (classic division ‒ always returns a float), % (modulus ‒ divides left operand by right operand and returns the remainder of the operation, e.g., 5 % 2 = 1), ** (exponentiation ‒ left operand raised to the power of right operand, e.g., 2 ** 3 = 2 * 2 * 2 = 8), // (floor/integer division ‒ returns a number resulting from division, but rounded down to the nearest whole number, e.g., 3 // 2.0 = 1.0)
    4. A unary operator is an operator with only one operand, e.g., -1, or +3.
    5. A binary operator is an operator with two operands, e.g., 4 + 5, or 12 % 5.
    6. Some operators act before others – the hierarchy of priorities:
      1. the ** operator (exponentiation) has the highest priority;
      2. then the unary + and - (note: a unary operator to the right of the exponentiation operator binds more strongly, for example: 4 ** -1 equals 0.25)
      3. then *, /, //, and %;
      4. and, finally, the lowest priority: the binary + and -.
    7. Subexpressions in parentheses are always calculated first, e.g., 15 - 1 * (5 * (1 + 2)) = 0.
    8. The exponentiation operator uses right-sided binding, e.g., 2 ** 2 ** 3 = 256.

2.4.1 Variables 

  1. Variable的name的命名规则:
    1. 不以数字开头;
    2. 可以包含大写小写字母,数字以及_
    3. 必须以字母或者_下划线开头
    4. 大小写字母视为不同
    5. 不可用Python的保留单词 比如print
    6. 不可以包含空格
  2. 可以使用print() 去conbine text and variables using + to output strings and variables. 前后都是strings 和 Variables的情况下,就可以正常加;对于string来说加号是让string连在一起;对于可以运算的数字来说,+就是运算符;
  3. Compound assignment operators: var+=1  也就是var = var+1
  4. Round(x,0)
  5. Key takeaways:
    1. automatically when you assign a value to it for the first time. (2.1.4.1)
    2. Each variable must have a unique name - an identifier. A legal identifier name must be a non-empty sequence of characters, must begin with the underscore(_), or a letter, and it cannot be a Python keyword. The first character may be followed by underscores, letters, and digits. Identifiers in Python are case-sensitive. (2.1.4.1)
    3. Python is a dynamically-typed language, which means you don't need to declare variables in it. (2.1.4.3) To assign values to variables, you can use a simple assignment operator in the form of the equal (=) sign, i.e., var = 1.
    4. You can also use compound assignment operators (shortcut operators) to modify values assigned to variables, e.g., var += 1, or var /= 5 * 2. (2.1.4.8)
    5. You can assign new values to already existing variables using the assignment operator or one of the compound operators, e.g.: (2.1.4.5)
    6. You can combine text and variables using the + operator, and use the print() function to output strings and variables, e.g.: (2.1.4.4)
  • 2.5.1 A comment on comments
  1.  Key takeaways:
    1. Comments can be used to leave additional information in code. They are omitted at runtime. The information left in source code is addressed to human readers. In Python, a comment is a piece of text that begins with #. The comment extends to the end of line.
    2. If you want to place a comment that spans several lines, you need to place # in front of them all. Moreover, you can use a comment to mark a piece of code that is not needed at the moment (see the last line of the snippet below), e.g.:
    3. Whenever possible and justified, you should give self-commenting names to variables, e.g., if you're using two variables to store a length and width of something, the variable names length and width may be a better choice than myvar1 and myvar2.
    4. It's important to use comments to make programs easier to understand, and to use readable and meaningful variable names in code. However, it's equally important not to use variable names that are confusing, or leave comments that contain wrong or incorrect information!
    5. Comments can be important when you are reading your own code after some time (trust us, developers do forget what their own code does), and when others are reading your code (can help them understand what your programs do and how they do it more quickly).

2.6.1 How to talk to a computer

  1. Anything = input(),注意input进来的东西是String,而不是integer或者float,不可以直接应用在数学运算里。
  2. 可以用int() 和float()把input进来的数字转换为整数或者浮点数
  3. +和*两个运算符在string中的用处:
    1. +在前后都是string的时候就变成了一个concatenator, 连接符,让前后的string连接起来
      1. 注意此处不能混合多种type,不能前面是int后面是string这种
    2. String * number:  * 会变成一个重复符号,让string重复number次
      1. "james" * 3   得到: "JamesJamesJames"
      2. 如果number 是 0, 就会得到一个empty string
  4. Str()可以让str(number)变成一个字符串
  5. Key takeaways:
    1. The print() function sends data to the console, while the input() function gets data from the console.
    2. The input() function comes with an optional parameter: the prompt string. It allows you to write a message before the user input, e.g.:
    3. When the input() function is called, t
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值