7 Python Fundamentals.docx
7 Python Fundamentals.docx
CHAPTER: 7
Python Fundamentals
1. Python Character Set:
Character set is a valid set of characters supported in Python. It consists of
● Letters: A-Z ,a-z
● Numbers: 0-9
● Special Symbols
● Whitespaces
● Other Characters: characters from Unicode Character set
2. TOKENS:
The smallest unit of program is considered as token. The various tokens supported in Python
are:
A.Keywords
B. Literals
C. Identifiers
D.Operators
E. Punctuators
A.Keywords:
Keywords are the reserved words taken from the English language that convey special
meaning to the computer. They are used for internal Python operations.
Eg break, True, False, def, etc.
B. Identifiers:
Identifiers are the name given to different parts of the program like variables, function
name, class name, list , tuple, etc.
Rules for defining an identifiers:
i. It starts with _ (underscore) and alphabet.
ii. Numbers are allowed but not in the first place.
iii. Special characters other than underscore are not allowed.
iv. It should not be a keyword.
v. It treats uppercase and lowercase differently.
Q. List the valid identifier name from the given set of identifiers.
a. _123 b. 1aqw c. A 1 d. a123 e. True f. BREAK
g. My.file h. qwe@ i. _q21
Solutions:
The valid identifiers are _123, a123, BREAK, _q21
C.Literals:
Literals are the constants that have fixed values. Python supports following types of
literals.
i. String: String is the collection of characters enclosed in single, double, triple
quotes.
● Single Line String: String that is defined in single(‘ ’) or double (“ ” )
quotes are called single line strings.
Ex:
X=’Q12’ #Valid
X=’12 #Error
34’
Single line string can be written in multiline by using \ back slash character. \
is escape sequence character.
X=’12\ #Valid
34’
● Multiline String: String that is defined in triple quotes is called multiline
string.
Eg:
X=’’’ Hello
World’’’
● Size of String literals: The len() function is used to compute the size of
string literals. It returns the total number of characters enclosed in quotes.
>>> x='Akash123'
>>> len(x)
8 #Total Characters is 8
>>> x='Akash\
123'
>>> len(x)
8
‘’’\ in single line string is used for multiline
does not take length’’’
>>> x='''Deepak
Kumar'''
>>> len(x)
12
#Enter or new line character takes 1 characters in
triple quotes
ii. Numeric Literals: Numeric literals are the numbers. They are divided into
● Integer Literals: Integer literals are the numbers without floating point. Ex:
20, -122, 8999. Integer Literal can be
✔Decimal: 90,78,-23
✔Octal integer: Octal integer prefix is 0o. Octal digits are 0 to 7.
Eg:
0o678 (Invalid) #8 is not octal
Oo234 (Valid)
0o234: 2*8^2+3*8^1+4X8^0=128+24+4= 156
✔Hexadecimal Integer: Prefix is 0x. Hexadecimal digits are 0-9,A-F.
Eg: 0x1AF (Valid)
0ABC (Invalid) #x is missing
● Floating Literals: Floating literals are the numbers with decimal point.
Floating literals can be
✔Fractional Notation: Eg 34.67 ,- 34.67
✔Exponent Notation: It is divided into two parts mantissa and
exponent.
Mantissa E exponent
234000 can be written as 2.34E+5
0.0234 can be written as 2.34E-2
2.96E2.3 #exponent cannot be floating
● Complex Literals: Python can also support complex numbers. It has two
parts- real and imaginary which can be written as
real+imaginary j . real property gives the real part and imag givesthe
imaginary part.
>>> x=3.6+4.5j
>>> x.real
3.6
>>> x.imag
4.5
Both real and imaginary are floating type.
iii. Special Literals: None is special literal which is use to represent nothing.
iv. Boolean Literals: True and False are the Boolean literals.
Ex:
X=True
D.OPERATORS:
Operators are the symbol that is used to perform operations on the set of operands.
Based on operands Python have two types of operator
● Unary Operator: It has only one operand. +(unary),-(unary), ~ bitwise
complement and notx are unary operators.
X=(+4)
X= -x
● Binary Operator: It has two operands. The binary operators are:
✔Arithmetic Operator:
+ is used for addition
- is used for subtraction
* is used for multiplication
/ is used for floating division
5/2=2.5
// is used for integer division
5//2=2
19//3=6
% is used for remainder
19%3=1
** is used for exponent
2**3=8
Precendence:
** : Right to left
*,/,//,%: left to right
Example:
>>> x=10
>>> y=20
>>> z=x+y
>>> z
30
>>> z=x-y
>>> z
-10
>>> z=x*y
>>> z
200
>>> z=x//5
>>> z
2
>>> z=x/5
>>> z
2.0
>>> z=x%3
>>> z
1
>>> z=x**3
>>> z
1000
✔Relational Operator: These operators are used to compare two
operands and return True or False value.
< (less than)
> (greater than)
<= (less than equal to)
>= (greater than equal to)
== (equality comparision)
!= (not equal)
7>=7=True
✔Logical Operator: and , or and not are the logical operators. and, or
are used to combine two conditions.
>>> a=True
>>> b=False
>>> a and not b
True
Precedence: BNAO (Brackets not and or)
✔Bitwise Operators: Bitwise operators are used to perform operations
on bits.
✔Shift Operator: << and >> are shift operators in python.
A>>2
A=12 = 1100=0011=3
A=16
A>>3 =10000=10=2
A=32
A>>3=100000=100=4
A=12
A<<3
A=1100 =1100000=96
E. PUNCTUATORS: Punctuators are the symbol that is used in various parts of the
program. It can be , , ; , : , {}, [] , ( ), @ , ‘ ’, “ “, etc.
● Comments: It begins with # and ‘’’ ‘’’. They are used for documentation purpose.
These lines are not interpreted by translator.
# is used for single line comment
‘’’ ‘’’: is used for multiline comment.
● Statements: They are set of instructions that perform some actions.
● Expressions: An expression is a legal set of symbol that represents a value.
X=y+z
P=q/r
q/r=p Invalid
p=q/r
qr=p
● Functions: Functions are the set of statements grouped together under one name.
They get execute when the call is made to them. They are defined using def
keyword.
Example:
def funex():
print("Funcion Calling")
print('Inside Function')
funex()
funex()
● Blocks and Indentation: Block refers to the line of code which are executed as a
part of other statements. Indentation is used to determine block in Python. The
different Python statements are:
Looping Statement (while loop, for loop)
Conditional Statement (if, elif, else)
Function definition
Class definition
● Variable And Assignment: Variable is the named memory locations that refers to
a value in Python. In Python, variable holds three properties.
id: Each variable hold a unique id based on value stored in it.
type: type of variable is determined on the basis of value stored in it.
value: The value refers to the data stored in variable.
>>> x='Amit'
>>> type(x)
<class 'str'>
>>> id(x)
50595800
>>> x
'Amit'
Declaring a Variable: Variable is declared automatically when value is
stored in it.
>>> x=10 #Single Assignment
>>> x,y,z=12,10,10 # Multiple Assignment
>>> x=y=z=30 # Assigning same value to
multiple variable
50595800 x y z
Variable in Python have dynamic typing. It means the id, type and value gets
updated dynamically on the basis of value stored in it.
Example:
>>> x='Amit'
>>> type(x)
<class 'str'>
>>> id(x)
50595800
>>> x
'Amit'
>>> x=234
>>> type(x)
<class 'int'>
>>> id(x)
8791121981760
>>> x
234
● lvalue: Expression that are required on the left hand
side of assignment operator
● rvalue: Expression that are required on the right hand
side of assignment operator.
z=p+q
Eg:
x=input(‘Enter a value’)
By default, the type of input is string. In order to convert the value in other
type constructor function should be used. The functions are
int() : to convert into integer type
float(): to convert into float type
str(): to convert into string type
complex(): to convert into complex type
boolean(): to convert into Boolean type
Note: The conversion is only possible when a valid literal is there.
Example:
x=input('Enter a value')
print(x, 'Type is',type(x))
x=int(input('Enter a value'))
print(x, 'Type is',type(x))
x=float(input('Enter a value'))
print(x, 'Type is',type(x))
x=bool(input('Enter a value'))
print(x, 'Type is',type(x))
x=complex(input('Enter a value'))
print(x, 'Type is',type(x))
SAMPLE INPUT
Enter a valueLucknow
Lucknow Type is <class 'str'>
Enter a value234
234 Type is <class 'int'>
Enter a value345.78
345.78 Type is <class 'float'>
Enter a value0
True Type is <class 'bool'>
Enter a value56.8+7.7j
(56.8+7.7j) Type is <class 'complex'>
BASIC PROGRAMS
1. WAP to input length and breadth and calculate area and perimeter of rect
l=float(input('Enter length'))
b=float(input('Enter breadth'))
a=l*b
p=2*(l+b)
print('area=',a)
print('Perimeter=',p)
2. WAP to input a radius of circle and compute area and circumference.(pi=3.14)
3. WAP to input the unit price and number of items and calculate cost.
4. WAP to input two numbers and swap their values
#a=10,b=20 a=20,b=10
print('x=',x,'y=',y)
z=x
x=y
y=z
print('x=',x,'y=',y)