CSE1021- Problem Solving and Programming -unit 2
CSE1021- Problem Solving and Programming -unit 2
and Programming
DATA, EXPRESSIONS, STATEMENTS
Variables
A variable allows us to store a value by assigning it to a name,
which can be used later.
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
Value should be given on the right side of assignment operator(=) and variable on left side.
>>>counter =45
print(counter)
>>> a=b=c=100
>>> a,b,c=2,4,"ram"
KEYWORDS:
> n = 17
> print(n)
Syntax:
print (expression/constant/variable)
COMMENTS:
It is a space given to the block of codes for class and function definitions or
flow control.
QUOTATION IN PYTHON
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals.
Anything that is represented using quotations are considered as string.
TUPLE ASSIGNMENT
• Python has a very powerful tuple assignment feature that allows a tuple of variables on
the left of an assignment to be assigned values from a tuple on the right of the
assignment.
• The left side is a tuple of variables; the right side is a tuple of values.
Example:
• It is useful to swap the values of two variables. With conventional assignment statements,
we have to use a temporary variable. For example, to swap a and b:
TUPLE ASSIGNMENT
• Tuple assignment solves this problem neatly:
(a, b) = (b, a)
• In tuple unpacking, the values in a tuple on the right are ‘unpacked’ into
the variables/names on the right:
TUPLE ASSIGNMENT
• The right side can be any kind of sequence (string,list,tuple)
OPERATORS
• Operators are the constructs which can manipulate the value of operands.
Arithmetic Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Arithmetic operators
• They are used to perform mathematical operations like addition,
subtraction, multiplication etc. Assume, a=10 and b=5
Comparison (Relational) Operators:
• Comparison operators are used to compare values.
• It either returns True or False according to the condition. Assume, a=10 and
b=5
Comparison (Relational) Operators:
• Comparison operators are used to compare values.
• It either returns True or False according to the condition. Assume, a=10 and
b=5
Assignment Operators
• Assignment operators are used in Python to assign values to variables.
Assignment Operators
Assignment Operators
Logical Operators
• Logical operators are the and, or, not operators.
Bitwise Operators:
• A bitwise operation operates on one or more bit patterns at the level of individual bits
• Let, x=[5,3,6,4,1]. To check particular item in list or not, in and not in operators are used.
Identity Operators
• They are used to check if two values (or variables) are located on the same part of the
memory