0% found this document useful (0 votes)
17 views4 pages

Python Fundamentals for Class 11

This document covers the fundamentals of Python programming, including tokens, keywords, identifiers, literals, escape sequences, and the structure of a Python program. It explains concepts such as expressions, statements, comments, variables, dynamic typing, input/output functions, and the print statement. The document serves as a comprehensive guide for beginners learning Python syntax and programming principles.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Python Fundamentals for Class 11

This document covers the fundamentals of Python programming, including tokens, keywords, identifiers, literals, escape sequences, and the structure of a Python program. It explains concepts such as expressions, statements, comments, variables, dynamic typing, input/output functions, and the print statement. The document serves as a comprehensive guide for beginners learning Python syntax and programming principles.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

Common questions

Powered by AI

The end parameter in print() specifies the string appended after the last value, generally the newline character by default ('\n'). By altering this parameter, output can be formatted to appear on the same line or separated by custom-ending strings, such as '$'. This offers flexibility in controlling how output presentations are arranged .

Escape sequences in Python use a backslash (\) followed by characters to represent special characters in strings, such as newline (\n) and tab (\t). These sequences count as a single character in string length calculations. For example, in the string "Amit\'s Pen", despite using an escape sequence for the apostrophe, the length remains ten .

Dynamic typing in Python allows a variable to point to data of any type, which can change during execution. For example, a variable initially assigned an integer value can later be reassigned to a string. This flexibility simplifies code and accommodates diverse data types without prior specification. For instance, a variable 'A' can first hold an integer value like 10 and later be reassigned to hold a string like 'Hello World' .

Python's multiple assignment allows assigning values to multiple variables simultaneously. It evaluates expressions on the right-hand side first and then assigns them to variables on the left-hand side. This feature reduces code complexity and enhances readability by allowing concise declaration and swapping of variable values, such as in `A, B = B, A` for swapping values .

Python allows several types of literals: String literals are sequences of characters within quotes (single, double, or triple). Integer literals represent integer values and can include decimal, octal, or hexadecimal forms. Floating point literals are numbers with decimal points and can include a sign. Boolean literals represent True or False values. None literals indicate the absence of a value and are used to signify the end of lists. They play crucial roles as they are immutable data types used to provide fixed values in a program .

The input() function in Python is crucial for obtaining user input, always returning a string type. This return type necessitates conversion to other data types for arithmetic operations, using functions like int() or float(). Failure to convert can lead to type errors when attempting operations that are incompatible with strings, such as addition with integers .

Unlike C++ or Java where variables are fixed storage locations, Python's variables function as references, meaning they point to data locations rather than containing data themselves. This model allows variables to change types dynamically and independently, offering greater flexibility and simplifying memory management for developers .

Python identifiers must start with a letter or an underscore, followed by any combination of letters, digits, or underscores. They are case-sensitive and cannot be a keyword or contain special characters. Violating these rules can lead to syntax errors; for instance, starting an identifier with a number or using Python keywords will result in errors .

Python uses indentation to define the scope of blocks of code, such as functions, loops, and conditionals. All the statements within a block must be indented at the same level. This method ensures clean and readable code structures, making it easier to understand and maintain, contrasting with languages like C++ where braces are used to define blocks .

A Python program's structure emphasizes readability and simplicity through indentation to define code blocks, unlike C++ or Java which use braces. Python lacks the concept of variable declarations; variables are dynamically typed and not storage containers with fixed locations. This allows for greater flexibility and reduces syntax complexity .

You might also like