Lecture 1
Lecture 1
@cse_bennett @csebennett
Today’s Outline
• Previous Session:
• Introduction to python
• Today’s Session:
• Basic Elements of Python Programs
• Tokens, Literals, Assignments.
• Identifiers, and Expressions.
• Scripts
2
Fundamentals of Python
Tokens
Comments Statements
Python
Fundamentals
Indention Constant
Variables
3
Tokens
Tokens are the smallest unit of the program.
Literals
Tokens
Reserved Words
Identifiers
Operators
4
Literals
• In the following example, the parameter values passed to the print
function are all technically called literals
• More precisely, “Bennett University” and “Welcome to Bennett” are called
textual literals, while 3 and 2.3 are called numeric literals
Value Based
Statements
7
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
8
Simple Assignment Statements
9
Simple Assignment Statements: What we
Think
• 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
10
Simple Assignment Statements: What
Actually Happen
• Python assignment statements are slightly different from the “variable as a
box” model
• In Python, values may end up anywhere in memory, and variables are used to
refer to them
x = 2.3
>>> x = 2 After
Before
>>> print(x) What will
2 2 happen to
x 2 x
>>> x = 2.3 value 2?
>>> print(x)
2.3
2.3
11
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 process x 2 X will be automatically
reclaimed by the
known as garbage collection garbage collector
2.3
12
Simultaneous Assignment
• Python allows us also to assign multiple values to multiple variables all at
the same time
>>> x, y = 2, 3
>>> x
2
>>> y
3
>>>
• This form of assignment might seem strange at first, but it can prove
remarkably useful (e.g., for swapping values)
13
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
>>> y = 3
>>> x = y
>>>
>>>
y
x
= x
X CANNOT be done with
two simple assignments
3
>>> y
3
14
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 >>> x = y three simple assignments,
are technically called
identifiers
>>> y = temp
>>> x
✓ but more efficiently with
simultaneous assignment
3
>>> y
2
>>> x,y = y,x
15
Identifiers
• 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 16
Identifiers
• 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
17
Identifiers
• 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 18
Identifiers
• 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
19
Expressions
• 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
20
Expressions
• 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
21
Expressions
• 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
22
Expressions
• You can produce new data (numeric or text) values in your program
using expressions
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Float Division
** Exponentiation
abs() Absolute Value
// Integer Division
% Remainder
24
Script
• One problem with entering code interactively into a Python shell is that
the definitions are lost when we quit the shell
• If we want to use these definitions again, we have to type them all over again!
• 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)
25
Programming Environments and IDLE
• A special type of software known as a programming environment
simplifies the process of creating modules/programs.
27
Summary
• A literal is a representation of a specific value (e.g., 3 is a literal
representing the number three)
• A variable is an identifier that stores a value, which can change (hence, the
name variable)
• 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)
28
Summary
• 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
29
Thank You
30