Please enable JavaScript.
Coggle requires JavaScript to display documents.
MODULE 2
Data types, variables, basic input-output operations, basic…
MODULE 2
Data types, variables, basic input-output operations, basic operators
Chapter 1 : Hello word
- how to write and run simple Python programs;
- what Python literals, operators, and expressions are;
- what variables are and what are the rules that govern them;
- how to perform basic input and output operations.
- The print() function is a built-in function. It prints/outputs a specified message to the screen/console window.
- 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.
- 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.
- Python strings are delimited with quotes, e.g., "I am a string" (double quotes), or 'I am a string, too' (single quotes).
- 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.
- 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. Any Keyword argument have to put after the last positional argument
- 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.
- Trong các chuỗi Python, dấu gạch chéo ngược () là một ký tự đặc biệt thông báo rằng ký tự tiếp theo có một ý nghĩa khác, ví dụ: \n (ký tự xuống dòng mới - newline character) bắt đầu một dòng xuất mới.
- Tham số vị trí (Positional arguments) là những tham số mà ý nghĩa của chúng được quyết định bởi vị trí của chúng, ví dụ: tham số thứ hai được xuất ra sau tham số thứ nhất, tham số thứ ba được xuất ra sau tham số thứ hai, v.v.
- Tham số từ khóa (Keyword arguments) là những tham số mà ý nghĩa của chúng không được quyết định bởi vị trí của chúng, mà bởi một từ đặc biệt (từ khóa - keyword) được sử dụng để xác định chúng. Bất kì Keyword Argurment nào đều phải đặt sau vị trí của last position argument
- Các tham số end và sep có thể được sử dụng để định dạng đầu ra của hàm print(). Tham số sep chỉ định ký tự phân tách giữa các tham số được xuất ra, ví dụ: print("H", "E", "L", "L", "O", sep="-"), trong khi tham số end chỉ định nội dung sẽ được in ở cuối câu lệnh print.
Chapter 4: Variables
1. Rule to give Name:
- A Variable is a named location reserved to store values in the memory. A variable is created or initialized automatically when you assign a value to it for the first time.
- Each variable must have a unique name - an identifier:
-- the name of the variable must be composed of upper-case or lower-case letters, digits, and the character _ (underscore)
-- It cannot be a Python keyword
-- First character may be followed by underscore, letter, but not a digit
-- upper- and lower-case letters are treated as differen
Từ vựng:
- parenthesis : dấu ngoặc đơn ()
- quotation mark: dấu nháy đơn ''
- fractional : phân số
Chapter 2: Literals
- A literals is data whose values are determined by the literal itself
- You use literals to encode data and to put them into your code.
- 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.").
- 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.
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.
3. 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
4. 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.
- 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.
Để có dấu ngoặc kép hoặc ngoặc đơn trong 1 chuỗi, ta cần thêm kí tự () hoặc mở và đóng string với kí tự đối lập
6. 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.
- There is one more, special literal that is used in Python: the None literal. This literal is a so-called NoneType object, and it is used to represent the absence of a value. We'll tell you more about it soon.
Mutability Matters!
Immutable Object: Interger, float, string, tuples
- Their value cannot be changed after they created
- If you perform an operation, that seem to modify an immutable object , you are actually creating a new object, and the variable is updated to point to the new object
Mutable Object: list, dictionary, Set
- Their value can be changed in place after they are created
- If you modify a mutable object using methods that change its content (like append(), pop(), sort()), the object itself is modified, and its ID remains the same.
- If two variables reference the same mutable object, changing the object through one variable will be visible when accessed through the other variable because they are both pointing to the same underlying data in memory.
- [:] and .copy() create shallow copies. For lists containing mutable objects, you might need copy.deepcopy() for a truly independent copy)
Chapter 3: Arithmetic Operator and the Hierachy of Priorities
Toán tử số học và hệ thống phân cấp ưu tiên
Operators: An operator is a symbol of the programming language, which is able to operate on the values
1. Exponentiation/Power (**)
- Raises the first number to the power of the second number
- When both arguments are integers, the result is an integer
- When at least one arguments is a float, the result is a float
2. Multiplication (*)
- Multiplies two numbers
- When both arguments are integers, the result is an integer
- When at least one arguments is a float, the result is a float
3. Division (/)
- The value in front of the slash is a dividend, the value behind the slash, a divisor
- The result produced by the division operator is always a float, regardless of whether or not the result seems to be a float
4. Integer Division / Floor Division (//)
- Divides the first number by the second and then rounds the result down to the nearest whole number
- Its result lacks the fraction part - its absent (for integers) or is always equal to zero (for float), this means that the results are always rounded
- It conforms to the integer vs float rule: integer by integer division gives an integer result. All other cases produce floats.
- Rounding is always goes to the lesser integer
- Kết quả của phép chia thiếu phần thập phân, phần này sẽ không có (đối với số nguyên) , hoặc luôn bằng 0 (đối với số thực), có nghĩa là kết quả luôn được làm tròn.
- Số nguyên chia số nguyên sẽ ra kết quả là số nguyên, các trường hợp khác đều ra số thực.
- Nó sẽ luôn luôn được làm tròn về số bé hơn (VD -1.5 sẽ được làm tròn thành -2)
5. Modulo / Remainder (%)
- The result of the operator is a remainder left after the integer division.
print(12 % 4.5) --> result = 3.0
Kết quả của toán tử này là phần dư (remainder) còn lại sau khi phép chia lấy phần nguyên (integer division)
6. Addition + Subtraction
- Unary operator is an operator with only on operand (-1, +3)
- Binary operator is an operator with two operand (4+5, 12%3)
- Toán tử một ngôi (unary operator) là toán tử chỉ có một toán hạng
- Toán tử hai ngôi (binary operator) là toán tử có hai toán hạng
Priority
Order of computations ( thứ tự tính toán)
- left-sided binding
- Right-sided binding: the exponentiation
- the ** operator has the highest priority
- then unary (+) and (-)
- then *, /, //, and %
- And finally the lowest priority is the binary (+), (-)
- == is operator compare the value of two operands
- Toán tử lũy thừa thực hiện từ phải sang trái
- Trong khi các toán tử khác đều lần lượt thực hiện từ trái sang phải
- Thứ tự ưu tiên:
-- Toán tử Lũy thừa có độ ưu tiên cao nhất,
-- Tiếp đến là toán tử một ngôi (unary operator)
-- tiếp theo là toán tử nhân, chia
- Cuối cùng là toán tử hai ngôi +, -
Expression: Formed when data (literals, variables) and operators are combined
Chapter 6:
Print, input
- print() function sends data to the console, while input() function gets data from the console
- The input() function comes with an optional parameter: the prompt string. It allows you to write a message before the user input
- When the input() function is called, the program's flow is stopped, the prompt symbol keeps blinking (it prompts the user to take action when the console is switched to input mode) until the user has entered an input and/or pressed the Enter key
- The result of the input() function is a string. You The result of the input() function is a string. You can add strings to each other using the concatenation (+) operator
- You can also multiply (* ‒ replication) strings