PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value
NUMERIC
LITERALS
int float complex
• Positive or negative whole numbers • Real numbers and are • are of the form a+bj,
with no decimal point written with decimal point where a and b are floats
• (called signed integers) • Commas not allowed j/J represents −1,
• Commas can’t appear in integer • VALID (2.0, -13.0, .3, 7.) where a is real and b is
constant • INVALID(7, +17/2 , imaginary part
• Types: decimal (4,-4), octal(0o23), 17.250.65 • Example: 2+3J
hexadecimal(0x2A3F)
PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value
BOOLEAN LITERALS None LITERAL
• None literal represents
True and False are the only absence of a value
two Boolean values • Indicates end of the list (a
data type)
PYTHON OPERATORS
Operators are tokens that trigger some computation/ action when
applied to variables and other objects in an expression
OPERATORS
UNARY BINARY
IDENTITY ARITHMETIC
LOGICAL RELATIONAL
MEMBERSHIP ASSIGNMENT
PYTHON PUNCTUATORS
Symbols that are used in programming languages to organize
programming sentence structures, and indicate the rhythm and
emphasis on expressions, statements and program structure.
‘ “ # \ () [] {} @ , : . =
PYTHON COMMENTS
• Comments are used to add a remark or a note in the source
code.
• Comments are not executed by interpreter.
• They are added with the purpose of making the source
code easier for humans to understand.
• They are used primarily to document the meaning and
purpose of source code and its input and output
requirements, so that we can remember later how it
functions and how to use it.
• Single line comment entry starts with #
PYTHON COMMENTS
• Multiline comment entry starts with
‘’’…..’’’
• For large and complex software, it
may require programmers to work
in teams and sometimes a
program written by one programmer
is required to be used or maintained
by other programmer. In such
situation documentation is required
in the form of comments
PYTHON VARIABLES
• A variable in python is uniquely identified by a name (identifier).
• In python, variable refers to an object- an item or object that is stored in the memory.
• Value of a variable can be a string(‘b’, “hello”) or numeric(123,5) or any combination of
alphanumeric characters (‘CDN76N’).
• We can use assignment statement to create new variables and assign specific values to
them.
• Variable declaration is implicit in python. It means variables are automatically declared
and defined when they are assigned a value the first time.
TRY YOURSELF
TRY YOURSELF
Front loaded
data space
print(id(num1))
print(id(num1))
num1 20
num2
num3 20.0
20.0
num4
num5 20.0
>>> i=0.1
>>> print(i*3==0.3)
False
>>> print(0.1+0.2)
0.30000000000000004
>>>
PYTHON VARIABLES [LVALUE AND RVALUE]
C=b
PYTHON VARIABLES
10
c b
PYTHON VARIABLES
Swapping (interchanging) values of variables
PYTHON VARIABLES
PYTHON VARIABLES
PYTHON VARIABLES
TASK: PYTHON VARIABLES
Write the output of the following:
a) num1 = 4
num2 = num1 + 1
num1 = 2
print (num1, num2)
b) num1, num2 = 2, 6
num1, num2 = num2, num1 + 2
print (num1, num2)
c) num1, num2 = 2, 3
num3, num2 = num1, num2 + 1
print (num1, num2, num3)
TASK: PYTHON VARIABLES
Write the output of the following:
a) num1 = 4 Ans:2 5
num2 = num1 + 1
num1 = 2
print (num1, num2)
b) num1, num2 = 2, 6
num1, num2 = num2, num1 + 2
print (num1, num2)
c) num1, num2 = 2, 3
num3, num2 = num1, num2 + 1
print (num1, num2, num3)
TASK: PYTHON VARIABLES
Write the output of the following:
a) num1 = 4 Ans:2 5
num2 = num1 + 1
num1 = 2
print (num1, num2)
b) num1, num2 = 2, 6 Ans:6 4
num1, num2 = num2, num1 + 2
print (num1, num2)
c) num1, num2 = 2, 3
num3, num2 = num1, num2 + 1
print (num1, num2, num3)
TASK: PYTHON VARIABLES
Write the output of the following:
a) num1 = 4 Ans:2 5
num2 = num1 + 1
num1 = 2
print (num1, num2)
b) num1, num2 = 2, 6 Ans:6 4
num1, num2 = num2, num1 + 2
print (num1, num2)
c) num1, num2 = 2, 3 Ans:2 4 2
num3, num2 = num1, num2 + 1 [2,4]
print (num1, num2, num3)
PYTHON VARIABLE DEFINITION
A variable is not
created until
some value is
assigned to it
Variable x created now
DYNAMIC TYPING
A variable pointing to a
value of a certain type, can
be made to point to a
value/object of different
type. This is called dynamic
typing.
In this case python will not
raise any error.
Type(object name) function is used to
determine the type of an object.
Object can be a variable or a literal.
Caution with DYNAMIC TYPING
Although python is
comfortable with changing
types of a variable, the
programmer is responsible
for ensuring right types for
certain types of operations.
TASK
Write the corresponding Python assignment statements:
a) Assign 10 to variable length and 20 to variable breadth.
>>>length,breadth=10,20
OR
>>>length=10
>>>breadth=20
b) Assign the average of values of variables length and breadth to a
variable sum.
>>>sum=(length+breadth)/2
c) Assign a list containing strings ‘Paper’, ‘Gel Pen’, and ‘Eraser’ to a variable
stationery.
>>>L=[‘Paper’, ‘Gel Pen’, ‘Eraser’]
>>>stationery=L
TASK
Write the corresponding Python assignment statements:
d) Assign the strings ‘Mohandas’, ‘Karamchand’, and ‘Gandhi’ to variables first, middle
and last.
>>>first,middle,last= ‘Mohandas’, ‘Karamchand’, ‘Gandhi’
OR
>>>first=‘Mohandas’
>>>middle=‘Karamchand’
>>>last= ‘Gandhi’
e) Assign the concatenated value of string variables first, middle and last to
variable fullname. Make sure to incorporate blank spaces appropriately between different
parts of names.
>>>fullname=first+ ‘ ‘+middle+ ‘ ‘+last
TASK
Write the corresponding Python assignment statements:
a) Assign 10 to variable length and 20 to variable breadth.
>>>length,breadth=10,20
OR
>>>length=10
>>>breadth=20
b) Assign the average of values of variables length and breadth to a
variable Sum.
>>>Sum=(length+breadth)/2
c) Assign a list containing strings ‘Paper’, ‘Gel Pen’, and ‘Eraser’ to a variable
stationery.
>>>L=[‘Paper’, ‘Gel Pen’, ‘Eraser’]
>>>stationery=L
TASK
Write the corresponding Python assignment statements:
d) Assign the strings ‘Mohandas’, ‘Karamchand’, and ‘Gandhi’ to variables first, middle
and last.
>>>first,middle,last= ‘Mohandas’, ‘Karamchand’, ‘Gandhi’
OR
>>>first=‘Mohandas’
>>>middle=‘Karamchand’
>>>last= ‘Gandhi’
e) Assign the concatenated value of string variables first, middle and last to
variable fullname. Make sure to incorporate blank spaces appropriately between different
parts of names.
>>>fullname=first+ ‘ ‘+middle+ ‘ ‘+last
TASK
Which data type will be used to represent the following data
values and why?
a) Number of months in a year
b) Resident of Delhi or not
c) Mobile number
d) Pocket money
e) Volume of a sphere
f) Perimeter of a square
g) Name of the student
h) Address of the student
TASK
Which data type will be used to represent the following data values and why?
a) Number of months in a year a) int
b) Resident of Delhi or not b) bool
c) Mobile number c) int/string
d) Pocket money d) float
e) Volume of a sphere e) float
f) Perimeter of a square f) float
g) Name of the student g) string
h) Address of the student h) string
Taking input from user
Use input() function to take data
from user
Though we are entering
numeric value for age,
but still it’s showing
string type. Hence, won’t
be able to do any
calculation on age
Taking input from user
Taking input from user
Taking input from user
Taking input from user
Taking input from user
Taking input from user
Taking input from user