CS1302 Introduction to Computer Programming
Simple examples of values
What is the characteristic of integer and string value?
All expressions in Python have a type. Sometimes it is denoted as its class. We can use a built-in function type to reveal the type of any Python expressions
How to operate on those values correctly?
How to have the computer remember the value?
The del keyword stands for delete. The del removes a variable's definition from the current interpreter session or an executing Python program
Python has strict rules for variable names. A variable name is one example of an identifier. An identifier is a word used to name things. One of the things an identifier can name is a variable. We will see other identifiers such as functions, class, and methods in the future
The details of rules:
| and | del | from | None | try |
|---|---|---|---|---|
| as | elif | global | nonlocal | True |
| assert | else | if | not | while |
| break | except | import | or | with |
| class | False | in | pass | yield |
| continue | finally | is | raise | |
| def | for | lambda | return |
Note that Python is case sensitive.
What about built-in functions such as print, type, int, str?
| Title | Storage | Smallest Magnitude | Largest Magnitude | Minimun precision |
|---|---|---|---|---|
| float | 64 bits | 2.22507e-308 | 1.79769e308 | 15 digits |
In addition to "normal‘’ characters such as alphabets, digits, punctuations, etc. We may embed special characters known as control codes. The backslash(\) signifies that the character that follows it is a control code, not a literal letter. The backslash(\) is known as the escape symbol.
For example, '\n' means the newline control code, '\t' stands for tab, '\b' for backspace, '\a' equals to alert control code.
How to input values into the computer and have it output those values?
How can we control the format of printing?
How can we have a string with multiple lines?
What is an expression?
An expression in a programming language is a syntactic entity that may be evaluated to determine its value. It is a combination of one or more constants, variables, functions, and operators that the programming language interprets and computes to produce another value. This process, for mathematical expressions, is called evaluation
| Expression | Meaning |
|---|---|
| x+y | x added to y, if x and y are numbers x concatenated to y, if x and y are strings |
| x-y | x take away y, if x and y are numbers |
| x*y | x times y, if x and y are numbers x concatenated with itself y times, if x is a string and y is an integer y concatenated with itself x times, if y is a string and x is an integer |
| x/y | x divided by y, if x and y are numbers |
| x//y | Floor of x divided by y, if x and y are numbers |
| x%y | Remainder of x divided by y, if x and y are numbers |
| x**y | x raised to y power, if x and y are numbers |
Do you know how the computer store floating-point number?
What issue it may cause?
Is there any solution?
Floating-point numbers are not real numbers, so the result of 1.0/3.0 cannot be represented exactly without infinite precession. In the decimal (base 10) number system, one-third is repeating fraction, so it has an infinite number of digits. Even simple nonrepeating decimal numbers can be a problem.
Can we operate with int and float together?
| Arity | Operators | Associativity |
|---|---|---|
| Binary | ** | Right |
| Unary | +,- | |
| Binary | *,/,//,% | Left |
| Binary | +,- | Left |
| Bianry | = | Right |
In mathematics, we usually use expressions like 3x+2y-5=0, 6a-4b+3c=9, x²+y²-1=0. However, Python has no implicit multiplication, which means that we must write 3x as 3*x. We may not omit the * operator.
In Python, we should write it likes:
3*x + 2*y -5 = 0,
6*a - 4*b + 3*c = 9,
x**2 + y**2 - 1 = 0.
(add space to improve the readability will not affect the result)
Keep writing comments to make your code more readable
Can we swap without the temporary variable?
Syntax Errors: When the interpreter detects an invalid program statement during the translation phase, it will terminate the program's execution and report syntax errors.
Run-time Errors: A syntactically correct Python program still can have problems. Some language errors happen only in certain run-time case.
Logic Errors: This is the error the interpreter can not detect. The errors happen because the logic written by programers is wrong. Such as dead-lock or infinite loop.
1.https://canvas.cityu.edu.hk/courses/30095/files/folder/Lectures
2.https://openlibrary.org/books/OL27680928M/Fundamentals_of_Python_Programming