Lecture 2
Values, Variables, Expressions, and Arithmetics
CS1302 Introduction to Computer Programming
Integer and String Value
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
Value Transformation
Value Addition
How to operate on those values correctly?
Variables
How to have the computer remember the value?
Assignment
The del keyword stands for delete. The del removes a variable's definition from the current interpreter session or an executing Python program
Identifier
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:
- An identifier must contain at least one character
- The first character of an identifier must be an alphabetic letter (upper or lower case) or underscore
- The remaining characters (if any) may be alphabetic letters (upper or lower case), underscores, or digits
- You can not use any reserved word to define your identifier
Reserved word
| 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?
Identifier and Build-in functions
Floating-point Number
| Title | Storage | Smallest Magnitude | Largest Magnitude | Minimun precision |
|---|---|---|---|---|
| float | 64 bits | 2.22507e-308 | 1.79769e308 | 15 digits |
Round and Truncation
Control Code
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.
Control Code Example
User Input
How to input values into the computer and have it output those values?
Controlling print function
How can we control the format of printing?
String Format
String Format
String Format
String Format
Muli-line String
How can we have a string with multiple lines?
Expression
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
Commonly Used Python Operators
| 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 |

Commonly Used Python Operators
Imprecision
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.


Mixed Expression
Can we operate with int and float together?
Operator Precedence and Associativity
| Arity | Operators | Associativity |
|---|---|---|
| Binary | ** | Right |
| Unary | +,- | |
| Binary | *,/,//,% | Left |
| Binary | +,- | Left |
| Bianry | = | Right |
Formating Expression
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)
Comments
Keep writing comments to make your code more readable
Arithmetic Example
More Arithmetic Operators
Swap
Can we swap without the temporary variable?
Errors
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.
Syntex Errors
Run-time Errors
References:
1.https://canvas.cityu.edu.hk/courses/30095/files/folder/Lectures
2.https://openlibrary.org/books/OL27680928M/Fundamentals_of_Python_Programming
CS1302 Lecture 2
By Chung Chan
CS1302 Lecture 2
- 106