Lecture 2
Lecture 2
Programming
Course instructor
Ayesha Kanwal
Variables, Expressions, and Data Types
Recap
• A computer is a universal information-processing machine, which
can carry out any process that can be described in sufficient
detail.
• There are many different languages, but all share the property of
having a precise syntax (form) and semantics (meaning)
• Types of Errors
• Basic Elements of Python Programs
• variables
• Literals
• Assignments
• Data-types
• Identifiers and Expressions
• Writing Simple Python Commands
5
Types of errors
• There are mainly three kinds of distinguishable errors in Python: syntax
errors, exceptions (run time error) and logical errors
6
Types of errors
• exceptions (run time error) Exceptions may occur in syntactically correct code
blocks at run time.
• Trying to read from a file which does not exist
• dividing a number by zero
• Logical errors:
• If you have logical errors, your code does not run as you expected
7
Elements Of A
Program
The Difference Between Brackets,
Braces, and
Parentheses
• ( ) Parentheses
•[ ] Brackets
•{ } Braces
Operators, Operands and expressions
• In Python, operators are special symbols that designate that some sort of
computation should be performed. The values that an operator acts on are called
operands.
• a = 10
• b= 10
• a+b
• In this case, the + operator adds the operands a and b together
• An expression is a combination of operators and operands that is interpreted to
produce some other value.
10
Simple
Expressions
• 1. 2300 +
20
• 2. 4800 /
12
• 3. 11 * 19
Values &
T ypes
• A value is one of the basic things a program works with.
• It can be a letter or a number.
• Example: Integer
• 14 String is a collection of alphabets,
• ‘Hello !! ’ String words or other characters.
• 13.235
Float
• Every value in Python has a type.
Values &
T ypes
• Printing Values:
• Syntax:
print (value)
Examples:
print (“Python is fun”) Python is fun
print (3.14) 3.14
Values &
Types
• Confirm the type from the interpreter:
• Syntax:
• type(value)
• Examples:
• >>> type (“Hello String”)
• >>> type (3.14)
• >>> type (130)
Values &
Types
• Self-Review Exercise:
Determine the type of the following value:
It’s a String….
Values &
Types
• When you have operands of two different types i.e. integer
and float result would automatically be converted to float.
• E.g.
• 2 + 4.0 = 6.0
• 15.0 / 3=???
Variable
s
• Variables are reserved memory locations to store values.
• The operand to the left of the = operator is the name of the variable and the
operand to the right of the = operator is the value stored in the variable.
Variable
s
Syntax:
Variable Value
• Example:
• name = “Moosa”
• miles= 25.6
Display the value of a
variable
Syntax(print value):
Print
(variable_name)
Values
X = X + 1;
X 10 +
= 11
Variable names and
keywords
1. Choose meaningful names for variables.
2. They can contain both letters and numbers, but they cannot start with a
number.
3. It is legal to use uppercase letters, but it is a good idea to begin variable names
with a lowercase letter.
4. The underscore character (_) can appear in a variable name.
5. Variable names can start with an underscore character, but generally it should
be avoided.
6. For variables with an illegal name, interpreter gives a syntax error.
7. Keywords can not be used as variable name.
Self-Review
Exercise
56House = “This is the 56th house”
number1 = 268
amount$= 56.89
pass = 10
Variable names and
keywords
Keywords:
The words reserved by the
python language.
Litera
ls
• In the following example, the parameter values passed to the print
function are all technically called literals
• More precisely, “Hello” and “Programming is fun!” are called textual literals,
while 3 and 2.3 are called numeric literals
>>> print("Hello")
Hello
>>> print("Programming is fun!")
Programming is fun!
>>> print(3)
3
>>> print(2.3)
2.3
Simple Assignment
Statements
• A literal is used to indicate a specific value, which can be assigned to
a variable
>>> x = 2
x is a variable and 2 is its value
>>> print(x)
2
>>> x = 2.3
>>> print(x)
2.3
Simple Assignment
Statements
• A literal is used to indicate a specific value, which can be assigned to
a variable
>>> x = 2
x is a variable and 2 is its value
>>> print(x)
2
x can be assigned different values;
>>> x = 2.3
hence, it is called a variable >>> print(x)
2.3
Simple Assignment Statements:
Box View
• A simple way to view the effect of an assignment is to assume that
when a variable changes, its old value is replaced
>>> x = 2 x = 2.3
Before After
>>> print(x)
2 x 2 x 2.3
>>> x = 2.3
>>> print(x)
2.3
Simple Assignment Statements:
Actual View
• In Python, values may end up anywhere in memory, and variables are used to
refer to them
x = 2.3
>>> x = 2
Before After
>>> print(x) What will
2 2 happen to
x 2 x
>>> x = 2.3 value 2?
>>> print(x)
2.3
2.3
Garbage
Collection
• Interestingly, as a Python programmer you do not have to worry about
computer memory getting filled up with old values when new values
are assigned to variables
After
Memory location
• Python will automatically clear old
values out of memory in a x 2 X will be automatically
reclaimed by the
process known as garbage garbage collector
collection
2.3
Assigning
Input
• So far, we have been using values specified by programmers and printed
or assigned to variables
• How can we let users (not programmers) input values?
>>> x = 2
>>> y = 3
>>> x = y
>>> y = x
>>> x X CANNOT be done with
two simple assignments
3
>>> y
3
Simultaneous
Assignment
• Suppose you have two variables x and y, and you want to swap their
values (i.e., you want the value stored in x to be in y and vice versa)
>>> x = 2
Thus far, we have been using >>> y = 3
different names for >>> temp = x CAN be done with
variables. These names three simple assignments,
>>> x = y
are technically called >>> y = temp but more efficiently with
identifiers >>> x simultaneous assignment
3
>>> y
2
>>>
Identifier
s
• Python has some rules about how identifiers can be formed
• Every identifier must begin with a letter or underscore, which may be
followed by any sequence of letters, digits, or underscores
>>> x1 = 10
>>> x2 = 20
>>> y_effect = 1.5
>>> celsius = 32
>>> 2celsius
File "<stdin>", line 1
2celsius
^
SyntaxError: invalid
syntax
Identifier
s
• Python has some rules about how identifiers can be formed
• Identifiers are case-sensitive
>>> x = 10
>>> X = 5.7
>>> print(x)
10
>>> print(X)
5.7
Identifier
s
• Python has some rules about how identifiers can be formed
• Some identifiers are part of Python itself (they are called reserved words or
keywords) and cannot be used by programmers as ordinary identifiers
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Python Keywords
Identifier
s
• Python has some rules about how identifiers can be formed
• Some identifiers are part of Python itself (they are called reserved words or
keywords) and cannot be used by programmers as ordinary identifiers
>>> for = 4
File "<stdin>", line 1
An example… for = 4
^
SyntaxError: invalid
syntax
Expressio
ns
• You can produce new data (numeric or text) values in your program
using expressions
>>> x = 2 + 3
This is an expression that uses the
>>> print(x)
addition operator 5
>>> print(5 * 7)
35
>>> print("5" + "7")
57
Expressio
ns
• You can produce new data (numeric or text) values in your program
using expressions
>>> x = 2 + 3
This is an expression that uses the
>>> print(x)
addition operator 5
>>> print(5 * 7)
This is another expression that uses the
35
multiplication operator >>> print("5" + "7")
57
Expressio
ns
• You can produce new data (numeric or text) values in your program
using expressions
>>> x = 2 + 3
This is an expression that uses the
>>> print(x)
addition operator 5
>>> print(5 * 7)
This is another expression that uses the
35
multiplication operator >>> print("5" + "7")
57
This is yet another expression that uses the
addition operator but to concatenate (or glue)
strings together
Expressio
ns
• You can produce new data (numeric or text) values in your program
using expressions
>>> x = 6 >>> print(x*y)
>>> y = 2 12
>>> print(x - y) >>> print(x**y)
Another 4 Yet another 36
example >>> print(x/y) example… >>> print(x%y)
… 3.0 0
>>> print(x//y) >>> print(abs(-x))
3 6
Expressions: Summary of
Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Float Division
** Exponentiation
abs() Absolute Value
// Integer Division
% Remainder
• A Python module file is just a text file with a .py extension, which can
be created using any program for editing text (e.g., notepad or vim)
Programming Environments
and IDLE
• A special type of software known as a programming environment
simplifies the process of creating modules/programs
• Operators are used to form and combine expressions into more complex
expressions (e.g., the expression x + 3 * y combines two expressions
together using the + and * operators)
Summa
ry
• In Python, assignment of a value to a variable is done using the equal
sign (i.e., =)
• Using assignments, programs can get inputs from users and manipulate
them internally
60