Chapter-7 (Python Fundamentals)
Chapter-7 (Python Fundamentals)
Python Fundamentals
Tokens: Basically, python tokens are the small units of the programming language. Python supports 5 types of Tokens.
Tokens
BY : FARAH ARIF 1
2) Arithmetic Operators: Arithmetic operators are used to perform mathematical operations
Symbol Description Example 1 Example 2
Addition >>>55+45 >>> “Good‟ + “Morning‟
+ 100 GoodMorning
_ Subtraction >>>55-45 >>>30-80
10 -50
Multiplication >>>55*45 >>> “Good‟* 3
* 2475 GoodGoodGood
Division >>>17/5 >>>28/3
/ 3 9
>>>17/5.0 >>> 17.0/5
3.4 3.4
% Remainder/ >>>17%5 >>> 23%2
Modulo 2 1
Exponentiation (Power) >>>2**3 >>>16**.5
** 8 4.0
// Floor Division >>>7.0//2 >>>3/ / 2
3.0 1
3) Relational Operators: Comparison operators are used to compare values. It either return True or
False according to the condition.
BY : FARAH ARIF 2
False >>> “Hello‟ != “Hello‟
False
== equal to >>>10==10 >>>”Hello‟ == “Hello‟
True True
>>>10==11 >>>‟Hello‟ == “Good Bye‟
False False
4) Logical Operators: They allow a program to make a decision based on multiple conditions.
Symbol Description
or If any one of the operand is true, then the condition becomes true.
and If both the operands are true, then the condition becomes true.
not Reverses the state of operand/condition.
5) Assignment Operators: Assignment operators are used in Python to assign values to variables.
Symbol Description Example Explanation
Assigned values from right >>>x=12 x=12
= side operands to left >>>y=‟greetings
variable ‟
+= added and assign back the >>>x+=2 Operator will change the
result to left operand value of x to 14 (x=x+2)
-= subtracted and assign >>>x-=2 x will become 12 (x=x-2)
back the result to left
operand
*= multiplied and assign back >>>x*=2 x will become 24 (x=x*2)
the result to left operand
/= divided and assign back >>>x/=2 x will become 12.0
the (x=x/2)
result to left operand
performed exponential >>>x**=2 x will become 144.0
**= (power) calculation on (x=x**2)
operators and assign value
to the left operand
performed floor division >>> x / /= 2 x will become 72.0
//= on (x=x/2)
operators and assign value
to the left operand
taken modulus using two >>>x%=2 x will become 0.0
%= operands and assign the (x=x%2)
result to left operand
BY : FARAH ARIF 3
6) Bitwise Operators and Shift Operators: Bitwise operators act on operands as if they were
string of binary digits. It operates bit by bit, hence the name.
In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
7) Identity Operators: They are used to check if two values (or variables) are located on the same part
of the memory.
8) Membership Operators: in and not in are the membership operators in Python. They are used to test
whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
Punctuators: Used to implement the grammatical and structure of a Syntax. Following are the python
punctuators- ‘ # \ “ . ( ) { } ` [ ] @ , : = etc.
Barebone of a python program:
#function definition Comment
def keyArgFunc(empname, emprole):
print ("Emp Name: ", empname) Function (indentation)
print ("Emp Role: ", emprole)
return;
A = 20 Expression
print("Calling in proper sequence")
keyArgFunc(empname = "Nick",emprole = "Manager" )
print("Calling in opposite sequence") Statements
keyArgFunc(emprole = "Manager",empname = "Nick")
BY : FARAH ARIF 4
A python program contains the following components:
1) Expression
2) Statement
3) Comments
4) Function
5) Block &n indentation
BY : FARAH ARIF 5