Getting Started with Python Class 11 Notes
Getting Started with Python Class 11 Notes
What is Program and Programming Language?
An ordered set of instructions to be executed by a computer to carry out a specific task is called a program, and the language used to specify this set of instructions to the computer is called a programming language.
Introduction to Python
Python Programming Language uses an interpreter to convert its instructions into machine language, so that it can be understood by the computer.
NOTE: Interpreter converts high level language to low level language line by line.
Features of Python
- Python is a high level language. It is a free and open source language.
- Python is case-sensitive language.
- Python is portable and platform independent.
- Python has a rich library of predefined functions.
- Python uses indentation for blocks and nested blocks.
Execution Modes of Python
There are two ways to use the Python interpreter:
- Interactive mode: In the interactive mode, we can simply type a Python statement on the >>> prompt directly. As soon as we press enter, the interpreter executes the statement and displays the result(s). This mode is convenient for testing a single line code for instant execution. But in the interactive mode, we cannot save the statements for future use and we have to retype the statements to run them again.
- Script mode: In the script mode, we can write a Python program in a file, save it and then use the interpreter to execute it. Python files has an extension “.py”. To execute the Python Program in script mode click Run–> Run Module from menu or press F5 from the keyboard.
Python Terminology
Keywords: Keywords are reserved words. Each keyword has a specific meaning to the Python interpreter, and we can use a keyword in our program only for the purpose for which it has been defined.
Identifiers: Identifiers are names used to identify a variable, function, or other entities in a program. The naming conventions of identifiers in Python are as follows
- An identifier cannot start with a digit.
- Keywords can not be used as identifiers.
- We cannot use special symbols like !, @, #, $, %, etc., in identifiers. (underscore can be used)
Examples of valid identifiers
- num1
- percentage
- mark1
- Stu_marks
Examples of invalid identifiers
- 1_num (reason: Starting from digit)
- marks@123 (reason : Using special character)
- pass (reason: Keyword )
Variables: Variable in Python refers to an object — an item or element that is stored in the memory. Value of a variable can be a string (e.g., ‘b’, ‘Global Citizen’), numeric (e.g., 345) or any combination of alphanumeric
characters (CD67). for example
marks = 56
name = “Ananya”
Variable declaration is implicit in Python, means variables are automatically declared and defined when they are assigned a value the first time. Variables must always be assigned values before they are used in expressions as otherwise it will lead to an error in the program.
Example 1: Write a program to store the names of three students and display them.
name1 = "Aman" name2 = "Sumit" name3 = "Ananya" print(name1) print(name2) print(name3) OUTPUT: Aman Sumit Ananya
Example 2: Write a program to display the sum of two numbers.
num1 = 20 num2 = 30 s = num1 + num2 print(s) OUTPUT: 50
Comments: Comments are used to add a remark or a note in the source code. Comments are not executed by interpreter. They are added with the purpose of making the source code easier for humans to understand. In Python, a comment starts with # (hash sign).
id( ) : This function returns the identity (memory address) of an object which remains same for the lifetime of that object. for example
num1 = 20 num2 = 20 print(id(num1)) print(id(num2)) OUTPUT: 8791510210176 #Your output can be different 8791510210176 #Both are showing same address as both variables are storing same number ie 20
Data Types :
Data type identifies the type of data values a variable can hold and the operations that can be performed on that data. Various data types in Python are as follows:
- Numbers
- Integer
- Boolean
- Floating Point
- Complex
- Integer
- Sequences
- Strings
- Lists
- Tuples
- Mappings
- Dictionary
Number
This data type stores numerical values only. It is further classified into three different types: int, float and complex.
Data type | Description | Examples |
int | This data type stores integers. | 4, -5, 23, -90 |
float | This data type stores floating point numbers. | -23.45, 40.26 |
complex | This data type stores complex numbers. | 1+2j, 21+5j |
Boolean data type (bool) is a sub type of integer. This data type stores two values, True and False. Boolean True value is non-zero. Boolean False is the value zero.
marks1 = 34 marks2 = 23.12 m1 = True print(type(marks1)) print(type(marks2)) print(type(m1)) OUTPUT: <class 'int'> <class 'float'> <class 'bool'> NOTE: type( ) function determine the data type of the variable.
Variables of simple data types like integers, float, boolean, etc., hold single values. But such variables are not useful to hold a long list of information, for example, marks of all students in a class test, names of the months in a year, names of students in a class. For this, Python provides data types like tuples, lists, dictionaries and sets.
Sequence :
It is an ordered collection of items, where each item is indexed by an integer. The three types of sequence data types available in Python are Strings, Lists and Tuples.
1. String : String is a group of characters(like alphabets, digits or special characters including spaces). Strings are enclosed in Single quotes(‘ ‘) or in double quotes(” “). for example
st1 = “Anuj”
st2 = ‘2342’
NOTE: Numerical functions can not be performed on String
2. List : List is a sequence of items enclosed in square brackets [ ] and items are separated by commas. for example
L1 = [23, ‘a’, 4, 2.3, ‘b’] #It is a list containing items of different data types.
L2 = [‘a’, ‘b’, ‘c’, ‘d’]
3. Tuple : Tuple is a sequence of items separated by commas and items are enclosed in parenthesis ( ).
T1 = (1, 3, 56, ‘s’, 5)
Difference between List and Tuple :
List | Tuple |
It is mutable | It is immutable |
Items are enclosed in [ ] | Items are enclosed in ( ) |
Mapping :
Mapping is an unordered data type in Python. Mapping data type in Python is Dictionary.
Dictionary : Dictionary in Python holds data items in key-value pairs. Items in a dictionary are enclosed in curly brackets { }. Every key is separated from its value using a colon (:) sign. The key : value pairs of a dictionary can be accessed using the key. for example:
D1 = {“a” : “Apple”, “b” : “Ball”, “c” : “Cat”} #”a”,”b”, “c” are keys and “Apple”, “Ball”, “Cat” are respective values
Mutable and Immutable Data Types
Variables whose values can be changed after they are created and assigned are called mutable. Variables whose values cannot be changed after they are created and assigned are called immutable. Python data types can be classified into mutable and immutable as shown below:
Immutable data types are:
- Integer
- Float
- Boolean
- String
- Tuple
Mutable data types are:
- List
- Sets
- Dictionary
Operators
Operators are special symbols which are used to perform some mathematical or logical operations on values. The values on which the operators work are called operands. for example in 34 + 31, here ‘+’ is a mathematical addition operator and the values 34 and 31 are operands. Python supports various kinds of operators like
1. Arithmetic Operators :
Arithmetic operators are used to perform the following Mathematical operations.
Operator Symbol | Operator Name | Explanation | Examples |
‘+’ | Addition | This operator help to add the two numeric values. This operator can also be used to concatenate two strings on either side of the operator | >>>5 + 9 14 >>>12+8 20 >>>’a’ + ‘b’ ab >>>’C’ + ‘S’ CS |
‘-‘ | Subtraction | This operator help to find the difference between two numbers. | >>>9 – 8 1 >>>130-31 99 |
‘*’ | Multiplication | This operator help us to find the product of two numeric values. | >>>7 * 4 28 >>>2 * 5 60 |
‘/’ | Division | This operator help us to divide the two numeric values and return the quotient with decimal. | >>>7/2 3.5 >>>8/2 4.0 |
‘%’ | Mod | This operator help us to divide the two numbers and return the remainder. NOTE: If first number is smaller than second, it will return the first number | >>>8%2 0 >>>40%6 4 >>>2%4 2 |
‘//’ | Floor Division | This operator divides the two numbers and return quotient without decimal. It is also called integer division. | >>>6//4 1 >>>9//2 4 |
‘**’ | Exponent | It performs exponential (power) calculation on operands | >>>2**2 4 >>>4**3 64 |
2. Relational Operators :
Relational Operators compares the values and return the boolean value.
Operator Symbol | Operator Name | Explanation | Examples |
> | Greater than | This operator returns True if the number on the left side of operator is larger than number on the right side. | >>>9 > 4 True >>>20 > 100 False |
< | Less than | This operator returns Tue if the number on the left side of operator is smaller than number on the right side. | >>>56 < 43 False >>>34 < 50 True |
>= | Greater than equals to | If the value of the left-side operand is greater than or equal to the value of the right-side operand, then condition is True, otherwise it is False | >>>20>=7 True >>>8>=10 False |
<= | Less than equals to | If the value of the left-side operand is less than or equal to the value of the right-side operand, then condition is True, otherwise it is False | >>>27<=27 True >>>15<=65 False |
== | Equal to | This operator returns True , if both the operands are equal. | >>>4==4 True >>>5==6 False |
!= | Not equal to | This operator returns True , if both the operands are not equal. | >>>7!=9 True >>>2!=2 False |
3. Assignment Operators :
This operator assigns or changes the value of the variable on its left.
Operator | Explanation | Examples |
= | Assigns value of right-side operand to left-side operand. | >>>x = 7 >>>x 7 >>>a = “India” >>>a ‘India’ |
+= | x += y is same as x = x + y It adds the value of right-side operand to the left-side operand and assigns the result to the left-side operand | >>>x = 9 >>>y = 10 >>>x += y >>>x 19 |
-= | x -= y is same as x = x – y It subtracts the value of right-side operand from the left-side operand and assigns the result to the left-side operand | >>>x = 20 >>>y = 10 >>>x -= y >>>x 10 |
*= | x *= y is same as x = x * y It multiply the value of right-side operand and left-side operand and assigns the result to the left-side operand | >>>x = 2 >>>y = 8 >>>x *= y >>>x 16 |
/= | x /= y is same as x = x / y It divides the value of right-side operand by the left-side operand and assigns the result to the left-side operand. | >>>x = 12 >>>y = 6 >>>x /= y >>>x 2.0 |
//= | x //= y is same as x = x // y It performs floor division using two operands and assigns the result to left-side operand. | >>>x = 12 >>>y = 6 >>>x //= y >>>x 2 |
**= | x **= y is same as x = x ** y It performs exponent operation using two operands and assigns the result to left-side operand. | >>>x = 2 >>>y = 5 >>>x **= y >>>x 32 |
%= | x %= y is same as x = x % y It performs modulus operation using two operands and assigns the result to left-side operand. | >>>x = 12 >>>y = 6 >>>x %= y >>>x 0 |
4. logical Operators :
There are three logical operators in Python. The logical operator evaluates to either True or False
Operator | Explanation | Examples |
and | It returns True if all the conditions are True | >>>x = 7 >>>y = 9 >>>x >5 and y >7 True >>>x<10 and y >10 False |
or | It returns True if any one of the condition is True | >>>x = 7 >>>y = 4 >>>x >5 or y >7 True >>>x >15 or y >7 False |
not | It negates the truth. It makes True to False and False to True. | >>>x = 7 >>>y = 4 >>>not(x >5 or y >7) False |
5. Identity Operators :
Identity operators is used to determine whether two variables are referring to the same object or not. There
are two identity operators.
Operator | Explanation | Examples |
is | Evaluates True if the variables on either side of the operator point towards the same memory location and False otherwise | >>>n1 = 5 >>>n2 = n1 >>>id(n1) 1423451526 >>>id(n2) 1423451526 >>>n1 is n2 True |
is not | Evaluates to False if the variables on either side of the operator point to the same memory location and True otherwise. | >>>n1 = 5 >>>n2 = n1 >>>id(n1) 1423451526 >>>id(n2) 1423451526 >>>n1 is not n2 False |
6. Membership Operators :
Membership operators are used to check if a value is a member of the given sequence (List, String, Tuple etc.) or not. There are two membership operators in Python
Operators | Explanation | Examples |
in | Returns True if the variable/value is found in the specified sequence and False otherwise | >>>’a’ in ‘amit’ True >>>1 in [11, 22, 33, 44] False |
not in | Returns False if the variable/value is found in the specified sequence and True otherwise | >>>’a’ not in ‘amit’ False >>>1 not in [11, 22, 33, 44] True |
Expressions :
An expression is defined as a combination of constants, variables, and operators. An expression always
evaluates to a value.
- 3+4 * (34 – 23)
- 34 %3 + 3
Evaluation of expressions :
Evaluation of the expression is based on precedence of operators. (It determines which operator will be evaluated first). Higher precedence operator is evaluated before the lower precedence operator.
The following table lists precedence of all operators from highest to lowest.
Order of Precedence | Operators | Description |
1 | ** | Exponentiation (raise to the power |
2 | *, /, %, // | Multiply, divide, modulo and floor division |
3 | +, – | Addition and subtraction |
4 | <= , < , > , >=, == , != | Relational and Comparison operators |
5 | =, %=, /=, //=, -=, +=, *=, **= | Assignment operators |
6 | is, is not | Identity operators |
7 | in, not in | Membership operators |
8 | not | Logical operators |
9 | and | Logical operators |
10 | or | Logical operators |
NOTE: The expression within bracket () is evaluated first. The expression is evaluated from left to right for operators with equal precedence.
Q1. Evaluate the following expressions
- 12 + 32 – 20
- 25 * 2 – 8
- 50 % 2 +3
- (25 – 20) * 2
Ans.
- 24
- 42
- 3
- 10
input( ) function :
This function helps to take or accept data from the user. The syntax for input( ) is:
input ([“Any message which you want to display”])
for example:
>>>n1 = input("Enter any number : ") Enter any number : 8 >>>age = input("Enter your age : ") #input( ) function takes every thing as string Enter your age : 23 >>>type(age) <class 'str'> >>>age = int(input("Enter your age : ")) #int( ) function convert string to integer Enter your age : 23 >>>type(age) <class 'int'>
print( ) function :
This function help to display message or text on screen. The syntax for print() is:
print(value [, …, sep = ‘ ‘])
sep: The optional parameter sep is a separator between the output values. We can use a character, integer or a string as a separator. The default separator is space. for example
>>>print("Hello","World","India") #by default value of sep is space ' ' Hello World India >>>print("Hello","World","India", sep = "@") Hello@World@India
Type Conversion :
In Python we can change the data type of a variable in Python from one type to another. Such data type conversion can happen in two ways: either explicitly (forced) or implicitly.
Explicit Conversion :
When the programmer specifies for the interpreter to convert a data type to another type. Explicit conversion, also called type casting. It happens when data type conversion takes place because the programmer forced it in the program. The general form of an explicit data type conversion is:
(new_data_type) (expression)
With explicit type conversion, there is a risk of loss of information since we are forcing an expression to be
of a specific type. for example
>>>x = 9.56 >>>y = int(x) >>>y 9 NOTE: In above example decimal part will be discarded
Explicit type conversion functions in Python
Function | Description |
int(x) | Converts x to an integer |
float(x) | Converts x to a floating-point number |
str(x) | Converts x to a string representation |
chr(x) | Converts ASCII value of x to character |
ord(x) | returns the character associated with the ASCII code x |
Implicit Conversion :
Implicit conversion, also known as coercion, happens when data type conversion is done automatically by
Python and is not instructed by the programmer.
n1 = 7 #n1 is an integer n2 = 2.0 #n2 is a float res = n1 - n2 #res is difference of a float and an integer print(res) print(type(res)) OUTPUT: 5.0 <class 'float'>
NOTE: In above example variable ‘res’ was automatically converted to a float value.
Debugging :
We can make some mistakes while writing a program, due to which, the program may not execute or may give wrong output. The process of identifying and removing such mistakes (called bugs), from a program is called debugging. Errors in the programs can be classified into three categories.
1. Syntax Error:
When we are not writing the programs according to the rules that determine its syntax then Syntax error occurs. If any syntax error is present, the interpreter shows error message(s) and stops the execution there only. for example (1 + 2) is syntactically correct, whereas (9 -2 is not due to absence of right parenthesis.
2. Logical Error:
An error that causes the program to behave incorrectly. Logical error produces an undesired output but without abrupt termination of the program.
Logical errors are also called semantic errors as they occur when the meaning of the program (its semantics)
is not correct. for example code to find the average of two numbers
>>> 12 + 6/2 #This code will not give the correct output so statement is logically incorrect
>>>15 # This is not the average of 12 and 6
3. Runtime Error:
A runtime error causes abnormal termination of program. Runtime error is when the statement is syntactically correct, but the interpreter cannot execute it. for example Division by Zero is most common runtime error
SUMMARY 1. Python is an open-source, high level, interpreter based language that can be used for a multitude of scientific and non-scientific computing purposes. 2. Comments are non-executable statements in a program. 3. An identifier is a user defined name given to a variable or a constant in a program. 4. The process of identifying and removing errors from a computer program is called debugging. 5. Trying to use a variable that has not been assigned a value gives an error. 6. There are several data types in Python — integer, boolean, float, complex, string, list, tuple, sets, None and dictionary. 7. Datatype conversion can happen either explicitly NOTES or implicitly. 8. Operators are constructs that manipulate the value of operands. Operators may be unary or binary. 9. An expression is a combination of values, variables and operators. 10. Python has input() function for taking user input. 11. Python has print() function to output data to a standard output device.
Class 11 : NCERT Solutions
Ch5 : Getting Started with Python
Ch10 : Tuples and Dictionaries
MCQ of Computer Science Chapter Wise
2. Flow of Control (Loop and Conditional statement)
3. 140+ MCQ on Introduction to Python
4. 120 MCQ on String in Python
7. 100+ MCQ on Flow of Control in Python
8. 60+ MCQ on Dictionary in Python
Important Links
100 Practice Questions on Python Fundamentals
120+ MySQL Practice Questions
90+ Practice Questions on List
50+ Output based Practice Questions
100 Practice Questions on String
70 Practice Questions on Loops
70 Practice Questions on if-else
Disclaimer : I tried to give you the simple ”Getting Started with Python Class 11 Notes” , but if you feel that there is/are mistakes in the code or explanation of “Getting Started with Python Class 11 Notes“ given above, you can directly contact me at csiplearninghub@gmail.com. Reference for the notes is NCERT book.
👍 👍 👍 👍 👍 nice 👍👍👍 👍👍