UNIT - II
CHAPTER – 3 (Python Fundamentals)
Token: The smallest individual unit in a program is known as a Token or lexical unit. For example –
keywords, identifiers, literals, operators, punctuators.
Keyword: The words that are reserved for programming language and must not be used as identifier
names. Python has the following keywords:
False del for in
None continue from is
True def global nonlocal
and elif if not
as else im po r t raise
break except or return
class finally pass try
while with yie ld
Identifiers (Names): These are building blocks of a program and are used for the name given to different
parts of the program. Identifier forming rules are:
1. It a long sequence of letters and digits.
2. The first character must be a letter or an underscore.
3. Python is case sensitive as it treats lower-case and upper case characters differently.
4. It must not be a keyword of python.
5. It can not contain any special character except underscore.
6. The digits 0 to 9 can be part of identifier except for the first character.
Example: Myfile, Date_77, Z2T 0Z, MYFI LE, _DS, File13, _HK3_J H //valid
Data -REC, 29_CLC, bre ak , My. Fi le, 13Fi le, while, 12#Num //Invalid
Literals/Values: These are data items that have a fixed value. Python allows following literals:
1. String Literals: A string literal is a sequence of characters surrounded by quotes (single or double or
triple). Example: ‘Astha’, “Welcome”
2. Integer Literals: An integer constant must have at least one digit and must not contain decimal
point. It may contain + or -. Python allows decimal integer, octal integer, and hexadecimal integer.
Example : +97 or -77
3. Floating Point Literals: It must have at least one digit before a decimal point and one digit after
decimal point. It may contain + or -. Example : +17.5 or -13.8
4. Boolean Literals: It is used to represent one of the two Boolean values True or False.
5. None Literals: The None literal is used to indicate absence of value. It is also used to indicate the
end of lists in python. The None value means there is no useful information.
Escape Sequence: It represents single character and consume 1 byte. It is represented by a back slash (\)
followed by one or more characters. These characters can be represented by using escape sequences.
\a Audible bell \” Double Quote
\n New line \’ Single Quote
\t Horizontal tab \r Carriage return
Size of String: Python determines the size of a string as the count of characters in the string. If a string has
an escape sequence contained within it, it will be counted as one character. To check the size of a string, we
can use len(Text) command on the python prompt.
Example: Text = ‘ abc’ #S ize is 3
Text = “Amit\ ’s Pen ” #S ize is 10
Prepared By: Manish Kumar Sharma (PGT Computer) Class 11th-Python Notes Page|5
Structure of Python Program:
# This is first prog ram of python # Comments
def Seeyou(): # Function
Print( “Good bye ”)
A=15
B = A – 10 # Statem ent and Expression
if B> 5:
print( “A is great er tha n 15”)
else:
print( “A is less than 15 ”)
Seeyou() # Calling function
1. Expression: An expression is any legal combination of symbols that represents a value. It represents
something, which Python evaluates and which then produces a value.
A – 10, (3+5)/4
2. Statement: A statement is a programming instruction that does something i.e. some action takes
place.
print(“Hello”), if B> 5:
3. Comments: Comments begins with symbol # and end with physical line. Comments enclosed in
triple quotes (“ ” ”) or (‘ ‘ ‘) are called docstrings.
4. Block and Indentation: A group of statements which are part of another statement or a function are
called block or code-block in python. To show block, python uses indentation. A block contains all
its statements at same indentation level.
if b>5:
print( “A is great er tha n 15”)
b=b*2
a=a+10
else:
print( “A is less than 15 ”)
a=a*2
b=b+10
Variables: A variable represents named location that refers to a value and whose values can be used and
processed during program run. A variable is not created until a value is assigned to it.
Name=’Ramesh’ #V ariable of string type
Age=16 #V ariable of Numeric type
print(B) #N a me error, B is not de fined
B=10
print(B) #output will be 10
Important: In Python, variables are not storage containers. Thus variables do not have fixed locations
unlike other programming language (C++, Java). The location they refer to, changes every time their values
change. Python variables are created by assigning value of desired type to them.
Prepared By: Manish Kumar Sharma (PGT Computer) Class 11th-Python Notes Page|6
Lvalues: The expression that can come on the left hand side of an assignment.
Rvalues: The expression that can come on the right hand side of an assignment.
A=20
B=10
20 =A #Error
A*2=B #Error
Multiple Assignments: In Python, assigning a value to a variable means, variable’s label is referring to that
value.
1. Assigning same values to multiple variables:
a = b = c = 20
2. Assigning multiple values to multiple variables:
X, Y, Z = 10, 20, 30 #A ssig n the va lues order wis e
A, B = 25, 50
print(A,B) #output =25 50
A, B = B, A
print(A,B) #output =50 25 (S wap Va lues)
While assigning values through multiple assignments, python first evaluates the RHS expression
and then assigns them to LHS.
A, B, C = 5, 10, 7
B, C, A = A+1, B+2, C-1
print(A,B,C) #outp ut =6, 6, 12 (B=6, C=12, A=6)
The expressions separated with commas are evaluated from left to right and assigned in same
order.
X=10
Y,Y=X+2, X+5
print(Y) #output =15
a,a=20,30
b,b=a+10, a+20
print(a,b) #output =30 50
Dynamic Typing: A variable pointing to a value of a certain type can be made to point to a value/object of
different type. This is called Dynamic Typing.
A=10
print(A) #output =10
B=A/2 #V alid, integers can be divided
A= ” Hello World ”
print(A) #output =Hello World
B=A/2 #Error, A string cannot be di vi ded
To determine the type of a variable, we can use type() in the following manner:
A=10
type(A) #output = clas s ‘int ’
A= ” hello ”
type(A) #output = clas s ‘str ’
Input Function: To get input from user, we can use built-in function input (). The input () function always
returns a value of string type. Python cannot add an integer to a string.
Name= input(“What is your name ?”) #Rames h
Age= input(“Enter your age ” ) #20
Age=Age+1 #Error, Age is string, not int
Python offers two functions int() and float() to be used with input() to convert the values received
through input:
Age=int(Age) OR Age=int(input(“Ente r your age”))
Age=Age+1 #output = 21
Age=int(input(“Ente r your age”)) #20.52, Value Error, not integer
Output through Print Statement: To send output to the output device, we can use built-in function print ().
A print () function without any value or name or expression prints a blank line.
print(“python is great ”) #output = python is gre at
print( “Sum of 2 and 3 is ”, 2+3) #output = Sum of 2 and 3 is 5
a=20
pr i n t ( “ Do u b l e of ” ,a , ” is ” ,a * 2 ) #outp ut = Doub le of a is 40
Prepared By: Manish Kumar Sharma (PGT Computer) Class 11th-Python Notes Page|7
Features of print Statement:
1. It auto-convert the items to strings.
2. By default print() takes value for end argument as ‘\n’ (newline character). If we explicitly give an
end argument with a print() function, then it will print the line that end with the string specified.
print(“python is great.”)
print(“I like it”)
#output = python is gre at.
I like it
print( “python is great. ”, end= ’ $’ )
print(“I like it”)
#outp ut = python is gre at. $I like it
3. The backslash (\) at the end of line means that the statement is still continuing in next line.
print(“Hello”, \
end = ‘ ’)
$###############$
Prepared By: Manish Kumar Sharma (PGT Computer) Class 11th-Python Notes Page|8