GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
PYTHON PROGRAMMING NOTES
Introduction to Python
Python(Part#1)
Program: An ordered set of instructions to be executed by a computer to carry out a specific task is
called a program.
Example: a=10 #instruction
b=20 #instruction
sum=a+b #instruction
print(sum) #instruction
Programming Language: The language used to specify this set of instructions to the computer is
called a programming language. Example: Python, C, C++, Java
Machine Language: As we know that computers understand the language of 0s and 1s which is
called machine language or low level language or Binary language.
Example: 1001100
0011110
High-Level Language: The language that used English like statements to write the computer
instructions. High-level
level languages are most widely programming languages because they are easy
to understand to human being. Examples: C, C++, Java, Python etc.
Note: Low Level Language is more friendly with machine and high level language is more friendly
with human.
Source Code: A program written in a high
high-level
level language is called source code.
a=10
b=20
sum=a+b
print(sum)
Source Code
Compiler: It is a translator that translates the high level language into low level language or
Machine Code (human-readable
readable code to a binary 0 and 1 bits language for a computer processor to
understand).
Compilers check all types of errors i.e Syntax error, semantic errors etc.
Scans the entire program and translates it as a whole into machine code.
Programming languages like C, C++ use compilers.
Interpreter: An interpreter processes the program statements one by one, first translating and then
executing. This process is continued until an error is encountered or the whole program is executed
successfully.
Programming languages like JavaScript, Python, Ruby use interpreters.
1 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
PYTHON PROGRAMMING NOTES
Python :
Python is a simple, general purpose, high level, and object
object-oriented programming language.
It is an interpreted language, as Python programs are executed by an interpreter.
Guido Van Rossum is known as the founder of Python programming.
It is a free and open source language.
Python is case-sensitive.
sensitive. For example, NAME and n
name
ame are not same in Python.
Python is portable and platform independent, means it can run on various operating systems
and hardware platforms.
Python has a rich library of predefined functions.
Python is also helpful in web development. Many popular web services and applications are
built using Python.
Working with Python
To write and run (execute) a Python program, we need to have a Python interpreter installed on our
computer or we can use any online Python interpreter. The interpreter is also called Python
Py shell.
There are two ways to use the Python interpreter:
a) Interactive mode b) Script mode
a) Interactive mode: To work in the interactive mode, we can simply type one command at a time and
python executes the
he command and gives you outpu
output as shown below.
But in the interactive mode, we cannot save the statements for future use and we have to retype the
statements to run them again.
b) Script Mode: In Script mode we can write more than one instruction in a file called python source
code file that can be executed. Python scripts are saved as files where file name has extension “.py”.
By default, the Python scripts are saved in the Python installation folder.
2 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
PYTHON PROGRAMMING NOTES
PYTHON 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. As Python is case sensitive,
keywords must be written exactly as given in Table
IDENTIFIERS
In programming languages, identifiers are names used to identify a variable, function, or other
entities in a program. The rules for naming an identifier in Python are as follows:
The name should begin with an uppercase or a lowercase alphabet or an underscore sign (_).
This may be followed by any combination of characters a a–z, A–Z, 0–99 or underscore (_). Thus,
an identifier cannot start with a digit.
It can be of any length. (However, it is preferred to keep it short and meaningful).
It should not be a keyword or reserved word given in above Table.
We cannot use special symbols like !, @, #, $, %, etc., in identifiers.
For example, to find the average of marks obtained by a student in three subjects, we can
ca choose
the identifiers as marks1, marks2, marks3 and avg rather than a, b, c, or A, B, C.
avg = (marks1 + marks2 + marks3)/3
Similarly, to calculate the area of a rectangle, we can use identifier names, such as area, length,
breadth instead of single alphabets
habets as identifiers for clarity and more readability.
area = length * breadth
VARIABLES
A Python variable is a reserved memory location to store values.
Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings,
Dictionary, etc.
Variable Naming Rules in Python
Variable name should start with letter(a
letter(a-zA-Z) or underscore (_).
In variable name, no special characters
acters allowed oother than underscore (_).
Variables are case sensitive. age and Age are different, since variable names are case sensitive.
Variable name can have numbers but not at the beginning.
Variable name should not be a Python keyword. Keywords are re also called as reserved words.
Example pass, break, continue.. etc are reserved for special meaning in Python. So, we should not declare
keyword as a variable name.
Valid Variables : name,_name , name1 ,Last_name
Invalid Variables: 1name , Last name , Name#
3 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
PYTHON PROGRAMMING NOTES
How to Declare and use a Variable
In Python we can use an assignment statement to create new variables and assign specific values
to them. Variables are nothing but reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory.
Variable declaration is implicit in Python, means variables are automatically declared and defined when they
are assigned a value the first time.
Let see an example:
a=100 str=”Data Mining hub Channel”
print (a) print(str)
OUTPUT : 100 OUTPUT: Data Mining Hub Channel
Multiple Assignment
Python allows you to assign a single value to several variables simultaneously.
For example −
a = b = c = 1 Here, an integer object is created with the value 1, and all three variables are assigned to
the same memory location.
You can also assign multiple e objects to multiple variables. For example −
a,b,c = 1,2,"john"
COMMENTS
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments starts with a #, and Python will ignore them.
Example:
print("Hello, World!") #This is a comment
Python does not really have a syntax for multiline comments.
To add a multiline comment you could insert a # for each line
print("Hello, World!") ) #This is
#a comment
comment.
EVERYTHING IS AN OBJECT
Python treats every value or data item as an object .
For example numeric, string etc all are an object for python.
All objects in Python has its own unique id.
The id is assigned to the object when it is created.
The id( ) function returns a unique id for the specified object.
The id is the object's memory address, and will be different for each time you run the
program.
Example
a = 20
print(id(a))
Output : 1433920576 #identity of a
b = 30
print(id(b))
Output: 1523821077
4 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
PYTHON PROGRAMMING NOTES
CLASS 11 COMPUTER SCIENCE WITH PYTHON NCERT SOLUTIONS
Question 1:Which of the following identifier names are invalid and why?
i Serial_no. v Total_Marks
ii 1st_Room vi total-Marks
Marks
iii Hundred$ vii _Percentage
iv Total Marks viii True
ANSWER:
i) Serial_no. : Invalid - Identifier in python cannot contain any special character except
underscore(_). It contain ( . ).
ii) 1st_Room: Invalid - Identifier in Python cannot start with a number.
iii) Hundred$: Invalid - Identifier in Python cannot contain any special character except
underscore(_).
iv) Total Marks: Invalid - Identifier in Python cannot contain any special character except
exce
underscore(_). It contain whitespace which is not allowed.
v) Total_Marks: Valid
vi) total-Marks: Invalid - Identifier in Python can cannot
not contain any special character except
underscore(_).
vii) _Percentage: Valid
viii) True: Invalid - a reserved keyword.
Question 2:
Write the corresponding Python assignment statements:
a) Assign 10 to variable length
gth and 20 to variable breadth.
b) Assign the average of values of variables length and breadth to a variable sum
c) Assign a list containing strings ‘Paper’, ‘Gel Pen’, and ‘Er
‘Eraser’
aser’ to a variable stationery.
d) Assign the strings ‘Mohandas’, ‘Karamchand’, and ‘Gandhi’ to variables first, middle
m and last.
e) Assign the concatenated value of string variables first, middle and last to variable fullname. Make
sure to incorporate blank spaces appropriately between different parts of names.
ANSWER:
a) length, breadth = 10,20
b) sum = (length + breadth)/2
c) stationaryy =['Paper','Gel Pen','Eraser']
d) first,middle,last = "Mohandas","Karamchand","Gandhi"
e) fullname = first +" "+ middle +" "+ last
References:
Class 11 Computer Science NCERT
https://www.w3schools.com/python/
5 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub