There are different value types.
- type: A category of values. The types we have seen so far are integers (type int), floating-point numbers (type float), and strings (type str).
- value: One of the basic units of data, like a number or string, that a program manipulates.
Examples:
- int - integer number i.e. 1, 2
- float - floating point number i.e. 1.0, 1.187694652
- str - string text i.e. "1", "Hello World!"
- bool - boolean (either true or false) i.e. True, False (note the case!)
- string: A type that represents sequences of characters.
- integer: A type that represents whole numbers.
- floating-point: A type that represents numbers with fractional parts.
Read more on Python's Built-in Functions
Variables represent values.
- variable: A name that refers to a value.
- assignment: A statement that assigns a value to a variable.
- x = 1
- x = 1.0
- x = "one"
- x = True
- mnemonic: A memory aid. We often give variables mnemonic names to help us remember what is stored in the variable.
- They have to begin with a letter (exception: variable names can start with an underscore character but this for is generally used for library code.)
- They can contain letters (both upper and lowercase) and numbers and the underscore character (_)
- They are case sensitive (it is suggested to use only lowercase letters in variable names) - just like everything else in Python
- They can be arbitrarily long
- keyword: A reserved word that is used by the compiler to parse a program.
Comments can be made on the program code.
- comment: Information in a program that is meant for other programmers (or anyone reading the source code) and has no effect on the execution of the program.
Examples:
- # This whole line is a comment
- x = 1 # The part before the "#" will run as a code, but the rest is a comment.
Statements
- statement: A section of code that represents a command or action.
- x = 3.5 # assignment statement
- print x # print statement
Operators and Operands
- operator: A special symbol that represents a simple computation like addition, multiplication, or string concatenation.
- operand: One of the values on which an operator operates.
Examples of numeric type operators:
- + performs addition
- - performs subtraction
- * performs multiplication
- / performs division (always in Python 3) or floor division depending on the value type (in Python 2)
- // performs always floor division
- % performs division and results the remainder of the division
- ** performs exponentiation
- floor division: The operation that divides two numbers and chops off the fraction part.
- modulus operator: An operator, denoted with a percent sign (%), that works on integers and yields the remainder when one number is divided by another.
- + performs concatenation
- concatenate: To join two operands end-to-end. I.e. "2" + "3" concatenates to "23"
Read more on sequence types and their operators.
Expressions
- expression: A combination of variables, operators, and values that represents a single result value.
- x # if x already has a value assigned
- x + 1 # if x already has a value assigned
- 1
- 20 + 32 * (x - 1) / (x * 60 ) + x * x / 60 * 5**2 - ( 5 + 9 ) * ( 15 - 7 )
- evaluate: To simplify an expression by performing the operations in order to yield a single value.
- x # will yield 5
- x + 1 # will yield 6
- 1 # will yield 1
- 20 + 32 * (x - 1) / (x * 60 ) + x * x / 60 * 5**2 - ( 5 + 9 ) * ( 15 - 7 ) # will yield -92 because of the rules of precedence.
- rules of precedence: The set of rules governing the order in which expressions involving multiple operators and operands are evaluated.
- Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even if it doesn’t change the result.
- Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is 3, not 27.
- Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.
- Operators with the same precedence are evaluated from left to right. So in the expression 5-3-1 is 1, not 3 because the 5-3 happens first and then 1 is subtracted from 2.
No comments:
Post a Comment