0% found this document useful (0 votes)
8 views

Chapter-7 (Python Fundamentals)

Chapter 7 covers Python fundamentals, focusing on tokens, which include keywords, identifiers, literals, operators, and punctuators. It details the rules for writing identifiers, various types of operators (arithmetic, relational, logical, assignment, bitwise, identity, and membership), and the structure of a Python program. Additionally, it provides examples and exercises for practicing Python programming.

Uploaded by

laraib786350
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Chapter-7 (Python Fundamentals)

Chapter 7 covers Python fundamentals, focusing on tokens, which include keywords, identifiers, literals, operators, and punctuators. It details the rules for writing identifiers, various types of operators (arithmetic, relational, logical, assignment, bitwise, identity, and membership), and the structure of a Python program. Additionally, it provides examples and exercises for practicing Python programming.

Uploaded by

laraib786350
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter – 7

Python Fundamentals
Tokens: Basically, python tokens are the small units of the programming language. Python supports 5 types of Tokens.

Tokens

Keywords Identifiers Literals Operators Punctuators

 Keywords: Keywords are pre-defined words use to perform special task.


 Identifiers: An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate
one entity from another.

Rules for writing identifiers:


 Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or
an underscore _. like myClass,var_1 and print_this_to_screen, all are valid example.
 An identifier cannot start with a digit. 1 variable is invalid, but variable1 is perfectly fine.
 Keywords cannot be used as identifiers.
 We cannot use special symbols like !, @, #, $, % etc. in our identifier.
 Identifier can be of any length.

 Literals: A literal is a number or string that appears directly in a program.


42 # Integer literal
3.14 # Floating-point literal
1.5j # Imaginary literal
'hello' # String literal
"world" # Another string literal
 Operators: Operators are special symbols which represents computation. They are applied on operand(s),
which can be values or variables. Same operator can behave differently on different data types. Operators
when applied on operands form an expression. Operators are categorized as Arithmetic, Relational, Logical
and Assignment. Value and variables when used with operator are known as operands.
Following is the partial list of operators:

1) Unary Operators: Unary operator works only on single operant.


Symbol Description Example 1 Example 2
+ Unary Plus Operator 50 +5
_ Unary Minus Operator -5

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.

Symbol Description Example 1 Example 2


Less than >>>7<10 >>>”Hello‟< ‟Goodbye‟
< True False
>>> 7<5 >>>'Goodbye'< 'Hello'
False True
Greater than >>>7>5 >>>”Hello‟> “Goodbye‟
> True True
>>>10<10 >>>'Goodbye'> 'Hello'
False False
<= less than equal to >>> 2<=5 >>>”Hello‟<= “Goodbye‟
True False
>>> 7<=4 >>>'Goodbye' <= 'Hello'
False True
>= greater than equal to >>>10>=10 >>>‟Hello‟>= “Goodbye‟
True True
>>>10>=12 >>>'Goodbye' >= 'Hello'
False False
!= not equal to >>>10!=11 >>>‟Hello‟!= “HELLO‟
True True
>>>10!=10

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)

Symbol Description Example


& Bitwise AND x& y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000 1110)
~ Bitwise NOT ~x = -11 (1111 0101)
^ Bitwise XOR x ^ y = 14 (0000 1110)
>> Bitwise right shift x>> 2 = 2 (0000 0010)
<< Bitwise left shift x<< 2 = 40 (0010 1000)

7) Identity Operators: They are used to check if two values (or variables) are located on the same part
of the memory.

Symbol Description Example


is True if the operands are identical condition becomes x is True
true.
Is not True if the operands are not identical x is not True

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).

Symbol Description Example


in True if value/variable is found in the sequence 5 in x
not in True if value/variable is not found in the sequence 5 not in x

 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

1) Expression: Which is evaluated and produce result. E.g. (20 + 4) / 4


2) Statement: Instruction that does something.
e.g
a = 20
print("Calling in proper sequence")
3) Comments: Which is readable for programmer but ignored by python interpreter.
 Single line comment: Which begins with # sign.
 Multi line comment: Write multiple lines in triple quote.
e.g
‘’’ this is
my first python
multiline comment
‘’’
4) Function: A code that has some name and it can be reused e.g.keyArgFunc in above program.
5) Block & indentation: group of statements is block. Indentation at same level create a block.e.g. all 3
statement of keyArgFunc function

Write a program for the following (in your notebook):


1) WAP to find out the simple interest of the values given by the user.
2) WAP to print this- “Hello, everyone “My name is ‘ALOK SINGH’ ” “
3) WAP to find out the Compound interest of the values given by the user.
4) WAP to find area of triangle, square, rectangle and circle.
5) WAP to calculate the total marks and percentage of 5 subjects (in float).
6) WAP to swap the value of 2 variables with the help of third variable.
7) WAP to swap the value of 2 variables without the help of third variable.
8) WAP to convert Kilometers in Meters.
9) Write a program that asks two people for their names; stores the names in variables called Name1 and Name2;
says hello to both of them.
10) WAP to that asks for your height in centimeters and then converts your height to feet and inches.
(1 foot=12 inches, 1 inch=2.54 cm)

BY : FARAH ARIF 5

You might also like