Python UNIT-1
Python UNIT-1
Identifiers are names for entities in a program such as class, variables and functions etc.
Rules for defining Identifiers:
Identifiers can be composed of uppercase, lowercase letters, underscore and digits bur
should start only with an alphabet or an underscore.
Identifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z)
or digits or an underscore.
Identifiers cannot start with digit
Keywords cannot be used as identifiers.
Only ( _ ) underscore special symbol can be used.
Valid Identifiers: sum total _ab_ add_1
Invalid Identifies: 1x x+y if
VARIABLES
A variable is nothing but a reserved memory location to store values. A variable in a
program gives data to the computer.
Ex:
>>>b=20
>>>print(b)
PYTHON INDENTATION
Python uses indentation. Block of code starts with indentation and ends with the
unintended line. Four whitespace character is used for indentation ans is preferred over tabs.
Ex:
x=1
if x==1:
print(“x is 1”)
Result:
x is 1
EXPRESSIONS
An Expression is a combination of values, variables and operators.
Ex:
>>>10+20
12
STATEMENTS
A Statement is an instruction that a python interpreter can execute.IN python enf of a
statement is marked by a newline character.
c=a+b
Multiline statement can be used in single line using semicolon(;)
>>a=1;b=10;c=a +b
Ex:
>>>b=20
>>>print(b)
>>>print(“\”Hello\””)
A statement is a complete line of code that performs some action, while an expression is
any section of the code that evaluates to a value. Expressions can be combined ―horizontally
into larger expressions using operators, while statements can only be combined vertically by
writing one after another, or with block constructs. Every expression can be used as a statement,
but most statements cannot be used as expressions
TUPLE ASSIGNMENTS
Tuple Assignment means assigning a tuple value into another tuple.
Ex:
t=(„Hello‟,‟hi‟)
>>>m,n=t
>>>print(m) Hello
>>>print(n) hi
>>>print(t) Hello,hi
In order to interchange the values of the two tuples the following method is used.
>>>a=(„1‟,‟4‟)
>>>b=(„10‟,‟15‟)
>>>a,b=b,a
>>>print(a,b)
((„10‟,‟15‟), („1‟,‟4‟))
COMMENTS
Comments are non-executable statements which explain what program does. There are
two ways to represent a comment.
Single Line Comment
Begins with # hash symbol
Ex:
>>>print(“Hello world”) # prints the string
1. Arithmetic Operator
It provides some Arithmetic operators which perform some arithmetic operations
Consider the values of a=10, b=20 for the following table.
Operator Meaning Syntax Description
+ Addition a+b It adds and gives the value 30
- Subtraction a-b It subtracts and gives the value -10
* Multiplication a*b It multiplies and gives the value 200
/ Division a/b It divides and gives the value 0.5
% Modulo a%b It divides and return the remainder 0
** Exponent a**b It performs the power and return 1020
// Floor a//b It divides and returns the least quotient
Example Program:
1. Write a Python Program with all arithmetic operators
>>>num1 = int(input('Enter First number: '))
>>>num2 = int(input('Enter Second number '))
>>>add = num1 + num2
>>>dif = num1 - num2
>>>mul = num1 * num2
>>>div = num1 / num2
>>>modulus = num1 % num2
>>>power = num1 ** num2
>>>floor_div = num1 // num2
>>>print('Sum of ',num1 ,'and' ,num2 ,'is :',add)
>>>print('Difference of ',num1 ,'and' ,num2 ,'is :',dif)
>>>print('Product of' ,num1 ,'and' ,num2 ,'is :',mul)
>>>print('Division of ',num1 ,'and' ,num2 ,'is :',div)
>>>print('Modulus of ',num1 ,'and' ,num2 ,'is :',modulus)
>>>print('Exponent of ',num1 ,'and' ,num2 ,'is :',power)
>>>print('Floor Division of ',num1 ,'and' ,num2 ,'is :',floor_div)
Output:
>>>
Enter First number: 10
Enter Second number 20
Sum of 10 and 20 is : 30
Difference of 10 and 20 is : -10
Product of 10 and 20 is : 200
Division of 10 and 20 is : 0.5
Modulus of 10 and 20 is : 10
Exponent of 10 and 20 is : 100000000000000000000
Floor Division of 10 and 20 is : 0
>>>
4. Logical Operator
Logical Operators are used to combine two or more condition and perform logical
operations using Logical AND, Logical OR, Logical Not.
Consider the values of a=10, b=20 for the following table.
Operator Example Description
AND if(a<b and a!=b) Both Conditions are true
OR if(a<b or a!=b) Anyone of the condition should be true
The condition returns true but not
NOT not (a<b)
operator returns false
5. Bitwise Operator
Bitwise Operator works on bits and performs bit by bit operation.
Consider the values of a=60, b=13 for the following table.
Operator Syntax Example Description
Binary AND It do the and operation
& a&b= 12
between two operations
Binary OR It do the or operation between
| a|b= 61
two operations
Binary Ones It do the not operation
~ ~a=61
Complement between two operations
<< Binary Left Shift <<a It do the left shift operation
>> Binary Right Shift >>a It do the right shift operation
A B A&B A|B ~A
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
Output:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is -61
Line 4 - Value of c is 240
Line 5 - Value of c is 15
6. Membership Operator
Membership Operator test for membership in a sequence such as strings, lists or tuples.
Consider the values of a=10, b=[10,20,30,40,50] for the following table.
Operator Syntax Example Description
value in String or If the value is „in‟ the list then
in a in b returns True
List or Tuple it returns True, else False
value not in String If the value is „not in‟ the list
not in a not in b returns False
or List or Tuple then it returns True, else False
Example:
x=‟python programming‟
print(„program‟ not in x)
print(„program„ in x)
print(„ Program„ in x)
Output:
False
True
False
7. Identity Operator
Identity Operators compare the memory locations of two objects.
Consider the values of a=10, b=20 for the following table.
Operator Syntax Example Description
If the variable 1 value is pointed
to the same object of variable 2
is variable 1 is variable 2 a is b returns False
value then it returns True, else
False
If the variable 1 value is not
variable 1 is not a is not b returns pointed to the same object of
is not
variable 2 False variable 2 value then it returns
True, else False
Example:
x1=7
y1=7
x2=‟welcome‟
y2=‟Welcome‟
print (x1 is y1)
print (x2 is y2)
print(x2 is not y2)
Output:
True
False
True
S. No Operators Description
1. () Parentheses
2. ** Exponent
3. +x, -x, ~x Unary plus, Unary minus, Bitwise NOT
4. *, /, //, % Multiplication, Division, Floor division, Modulus
5. +, - Addition, Subtraction
6. <<, >> Bitwise shift operators
7. & Bitwise AND
8. ^ Bitwise XOR
9. | Bitwise OR
is, is not, in, not in Comparison, Identity, Membership
10. ==, !=, >, >=, <, <=,
operators
11. not Logical NOT
12. and Logical AND
13. or Logical OR
If more than one operator exists in the same group. These operators have the same
precedence. When two operators have the same precedence, associativity helps to determine
which the order of operations. Associativity is the order in which an expression is evaluated that
has multiple operator of the same precedence. Almost all the operators have left-to-right
associativity. For example, multiplication and floor division have the same precedence. Hence, if
both of them are present in an expression, left one evaluates first.
Example:
>>> 10 * 7 // 3
23
>>> 10 * (7//3)
20
>>> (10 * 7)//3
23
10 * 7 // 3 is equivalent to (10 * 7)//3.