Programming in Python
Data and Expressions:
Literals, Variables and Identifiers
Today’s Outline
• Previous Session:
• Introduction to python
• Today’s Session:
• Basic Elements of Python Programs
• Tokens, Literals, Assignments.
• Identifiers, and Expressions.
• Scripts
• Hands on Session with Jupyter Notebook:
• We will practice on the Python basic elements in Google Colab.
2
Fundamentals of Python
Tokens
Comments Statements
Python
Fundamentals
Indentation 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, “Sharda University” and “Welcome to Sharda”
are called textual literals, while 3 and 2.3 are called numeric
literals
>>> print(“Sharda University")
Bennett University
>>> print(“Welcome to Sharda")
Welcome to Sharda
>>> print(3)
3
>>> print(2.3)
2.3
5
Literals
• String Literals:
• A string literal is a sequence of characters surrounded by quotes. We
can use both single, double or triple quotes for a string. And, a
String character literal is a single character surrounded by single or double
quotes.
Numeric • Numeric Literals:
Literals
Boolean • Numeric Literals are immutable (unchangeable). Numeric literals can
belong to 3 different numerical types Integer, Float, and Complex.
Collection • Boolean Literals:
• A Boolean literal can have any of the two values: True or False.
Special
• Collection literals:
• There are four different literal collections List literals, Tuple literals,
Dict literals, and Set literals.
• Special literals:
• Python contains one special literal i.e. None. We use it to specify to
6
Statements
• Python statements are nothing but logical instructions
that interpreter can read and execute. It can be both
single and multiline.
There are two categories of statements in
Python: Value Based
Statements
Simple Variable Based
Assignments
Multiple Operation based
Expression
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
• 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)
x can be assigned different values;
2
hence, it is called a variable >>> x = 2.3
>>>
print(x)
2.3
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) x 2 x 2.3
2
>>> 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
>>> What will
print(x) 2 happen to
x 2 x
2 value 2?
>>> x = 2.3
>>>
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
X
• Python will automatically clear old will be automatically
x 2
values out of memory in a process 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
>>>
>>>
3
y
x
= x
X CANNOT be done with
two simple assignments
>>> 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 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
False
programmers class
as ordinaryfinally
identifiersis 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
>>> x = 6 >>> print(x*y)
>>> y = 2 12
>>> print(x - >>> print(x**y)
Another y) Yet another 36
example… 4 example… >>> print(x%y)
>>> 0
print(x/y) >>> print(abs(-x))
3.0 6
>>>
print(x//y)
23
Expressions: Summary of Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Float Division
** Exponentiation
abs() Absolute Value
// Integer Division
% Remainder
Python Built-In Numeric Operations
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!
• To this end, programs are usually created by typing
definitions into a separate file called a module or script
• This file is saved on disk so that it can be used over and 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.
• A programming environment helps programmers write
programs and includes features such as automatic
indenting, color highlighting, and interactive development.
• The standard Python distribution includes a programming
environment called IDLE that you can use for working on
the programs of this course.
26
Summary
• Programs are composed of statements that are built from
identifiers and expressions
• Identifiers are names
• They begin with an underscore or letter which can be followed by
a combination of letter, digit, and/or underscore characters
• They are case sensitive
• Expressions are the fragments of a program that produce
data
• They can be composed of literals, variables, and operators
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
• Python allows simultaneous assignments, which are useful
for swapping values of variables
29
Thank You
30