Computer Programming
Variables, Expressions and Statements
Eyob S.
SITE, AAiT
May 27, 2024
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 1 / 34
Table of Contents
1 Variables, Expressions and Statements
2 Order of Operations
3 Data Types
4 TypeErrors and Type Conversions
5 Debugging
6 Comments
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 2 / 34
What is a Variable?
A variable is a name that refers to a value. We can use this name later
in our code to access the value.
An assignment statement "=" creates a new variable and gives it a
value:
x=3 - assign a value 3 to the variable named ‘x‘
message="hello" - assign a string value ”hello” to the variable named
‘message‘
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 3 / 34
What is a Variable? (continued...)
More formally a variable is a named place in the memory where a
programmer can store data and later retrieve the data using the variable
name
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.
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 4 / 34
What is a Variable? (continued...)
More formally a variable is a named place in the memory where a
programmer can store data and later retrieve the data using the variable
name
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.
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 5 / 34
Python Variable Name Rules
Must consist of letters, numbers, or underscores
Must start with a letter or underscore
Case Sensitive
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 6 / 34
Python Variable Name Rules (continued...)
Good: spam, result, result23, speed
Bad: 23spam, #sign, var.12
Different: spam, Spam, SPAM
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 7 / 34
Reserved Words (keywords)
The interpreter uses keywords to recognize the structure of the pro-
gram, and they cannot be used as variable names.
You don’t have to memorize this list. In most development environ-
ments, keywords are displayed in a different color
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 8 / 34
Mnemonic Variable Names
We name variables to help us remember what we intend to store in
them (“mnemonic” = “memory aid”)
x1q3z9ocd = 65.25
x1q3z9afd = 30
x1q3p9afd = x1q3z9ocd * x1q3z9afd
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 9 / 34
Mnemonic Variable Names (continued...)
a = 65.25
b = 30
c = a * b
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 10 / 34
Mnemonic Variable Names (continued...)
mass = 65.25
acceleration = 30
Force = mass * acceleration
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 11 / 34
Named Constants
They are similar to variables: a memory location that’s been given a
name.
Unlike variables their contents shouldn’t change.
PI = 3.14
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 12 / 34
Literal (unnamed constant)
A literal is a fixed value directly written in your code. It represents a
specific data element and cannot be changed during program execution.
Literals come in various forms depending on the data type they repre-
sent:
Numbers: 42, 3.14, -100
Strings: "Hello, world!"
Boolean values: True, False
Characters: ’a’, ’$’, ’&’
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 13 / 34
Expressions and Statements
An expression is a combination of values, variables, and operators.
A value all by itself is considered an expression, and so is a variable
42
x + 18
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 14 / 34
Expressions and Statements
A statement is a unit of code that has an effect, like creating a variable
or displaying a value.
n = 17 ⇒ assignment statement
n = 4 * x * ( 1 - x) ⇒ assignment statement
print(n) ⇒ print statement
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 15 / 34
Expressions and Statements
A statement is a unit of code that has an effect, like creating a variable
or displaying a value.
n = 17 ⇒ assignment statement
n = 4 * x * ( 1 - x) ⇒ assignment statement
print(n) ⇒ print statement
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 16 / 34
Arithmetic Operators
Table: Python Arithmetic Operators and Operations
Operator Operation Example
+ Addition 3 + 2
- Subtraction 3 - 2
* Multiplication 3 * 2
/ Division (float) 3 / 2
// Division (floor) 3 // 2
% Modulus (remainder) 3 % 2
** Exponentiation 3 ** 2
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 17 / 34
Order of Operations (operator precedence)
When an expression contains more than one operator, the order of
evaluation depends on the order of operations.
Table: Python Arithmetic Operator Precedence
Precedence Operators
() Parentheses
** Exponentiation
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 18 / 34
Order of Operations (operator precedence)
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
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 19 / 34
String Operations
The + operator performs string concatenation, which means it joins
the strings by linking them end-to-end.
'throat'+ 'warbler'⇒ 'throatwarbler'
The * operator also works on strings; it performs repetition.
'Spam'* 3 ⇒ 'SpamSpamSpam'
’2’ - ’1’ ⇒ ???
’eggs’ * ’easy’ ⇒ ???
’third’ * ’3’ ⇒ ???
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 20 / 34
Python Data Types
Type is a category of values.
In Python, data types specify the kind of values a variable can hold.
They determine how data is stored and manipulated in your code.
Understanding data types is essential for writing efficient and accurate
programs.
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 21 / 34
Python Data Types: Numeric Types
Numeric types are used to represent numbers.
There are three distinct numeric types:
int: integer numbers: -5, 0, 17
float: decimal numbers: -4.44, 0.86, 3.4, 5.0
complex: Complex numbers: -3 + 2j, 2.4 - 3j
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 22 / 34
Python Data Types: Text Type
The str type represents textual data.
Strings are sequences of characters enclosed in single quotes (') or
double quotes (”), or even triple quotes(”””).
Examples:
'Hello', "World", '2', 'a', '1+3'
'''This is a multi-line string'''
”””This is also a multi-line string”””
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 23 / 34
Python Data Types: Other Types
Sequence types: list, tuple, range
Mapping Type: dictionary
Set Types: set, frozenset
Boolean Types: True(1), False(0)
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 24 / 34
Mixing Integer and Floating
99 // 100
99 / 100
99 / 100.0, 99.0 // 100 ⇒ Type Casting
1 + 2 * 3 // 4.0 - 5
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 25 / 34
TypeError
message = 'hello '+ 'there'
type(message) ⇒ ???
message + 1 ⇒ ???
message + '1'⇒ ???
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 26 / 34
Type Conversions
num = '2 '+ '1'
type(num) ⇒ ???
result = int(num) + 1 ⇒ ???
type(result) ⇒ ???
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 27 / 34
Type Conversions (continued...)
num = 42
float(num) ⇒ ???
int(42.56) + 1 ⇒ ???
str(1) + str(2) ⇒ ???
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 28 / 34
Debugging
Programming errors are called bugs and the process of fixing these
errors is called debugging.
Three kinds of errors can occur in a program: syntax errors, runtime
errors, and semantic errors.
Syntax error: “Syntax” refers to the structure of a program and the
rules about that structure.
If there is a syntax error anywhere in your program, Python displays an
error message and quits, and you will not be able to run the program.
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 29 / 34
Debugging (continued...)
For example, parentheses have to come in matching pairs, so (1 + 2)
is legal, but 8) is a syntax error.
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.
For example, division by zero.
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 30 / 34
Debugging (continued...)
Semantic error: The third type of error is “semantic”, which means
related to meaning.
If there is a semantic error in your program, it will run without gener-
ating error messages, but it will not do the right thing.
For example, using // instead of /.
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 31 / 34
Comments
As programs get bigger and more complicated, they get more difficult
to read.
Formal languages are dense, and it is often difficult to look at a piece
of code and figure out what it is doing, or why.
For this reason, it is a good idea to add notes to your programs to
explain in natural language what the program is doing. These notes
are called comments, and they start with the # symbol:
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 32 / 34
Comments (continued...)
#compute the percentage of the hour that has elapsed
percentage = (minute * 100)/60
percentage = (minute * 100)/60 #percentage of an hour
v = 5 #assign 5 to v ⇒ not good
v = 5 #velocity in meters/second ⇒ good
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.
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 33 / 34
END
Thank You!!!
Eyob S. (SITE, AAiT) Computer Programming May 27, 2024 34 / 34