Chapter 2 Python
Chapter 2 Python
Eyob S.
SITE, AAiT
2 Order of Operations
3 Data Types
5 Debugging
6 Comments
A variable is a name that refers to a value. We can use this name later
in our code to access the value.
Programmers generally choose names for their variables that are mean-
ingful—they document what the variable is used for
Variable names can be as long as you like. They can contain both
letters and numbers, but they can’t begin with a number.
Programmers generally choose names for their variables that are mean-
ingful—they document what the variable is used for
Variable names can be as long as you like. They can contain both
letters and numbers, but they can’t begin with a number.
Case Sensitive
x1q3z9ocd = 65.25
x1q3z9afd = 30
a = 65.25
b = 30
c = a * b
mass = 65.25
acceleration = 30
PI = 3.14
Literals come in various forms depending on the data type they repre-
sent:
42
x + 18
n = 17 ⇒ assignment statement
n = 4 * x * ( 1 - x) ⇒ assignment statement
n = 17 ⇒ assignment statement
n = 4 * x * ( 1 - x) ⇒ assignment statement
Precedence Operators
() Parentheses
** Exponentiation
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
Operators with the same precedence are evaluated from left to right
(except exponentiation).
x = 1 + 2 * 3 - 4 / 5 ** 6
(3 * 100) / 60
3 * 100 / 60
4 / 2 * 8
4 / (2 * 8)
4 / 2 / 8
'Spam'* 3 ⇒ 'SpamSpamSpam'
In Python, data types specify the kind of values a variable can hold.
Examples:
99 // 100
99 / 100
1 + 2 * 3 // 4.0 - 5
type(message) ⇒ ???
message + 1 ⇒ ???
type(num) ⇒ ???
type(result) ⇒ ???
num = 42
float(num) ⇒ ???
int(42.56) + 1 ⇒ ???
Programming errors are called bugs and the process of fixing these
errors is called debugging.
Runtime error: this error does not appear until after the program has
started running.
These errors are also called exceptions because they usually indicate
that something exceptional (and bad) has happened.
As programs get bigger and more complicated, they get more difficult
to read.
Good variable names can reduce the need for comments, but long
names can make complex expressions hard to read, so there is a trade-
off.
Thank You!!!