Python Introduction
Python Introduction
Class IX
INTRODUCTION TO PYTHON
• Python is a high level, structured, open source programming language
that supports the development of a wide range of applications such
as simple text processing, World Wide Web browsers and games.
• Python was founded by Guido Van Rossum in early 1990s.
• It is named after “Monty Python’s Flying Circus’, a comedy television
show.
Features of Python
• Python is easy to learn and use
• It is an expressive language as it can perform complex tasks using a
few lines of code.
• It is an interpreted language that executes the code line-by-line one at
a time.
• It is a portable language as it can run equally well on different
platforms such Windows, Linux, Unix and Macintosh.
• It is an open source language and available free of cost.
APPLICATIONS OF PYTHOIN
Python Basics
• The print() function
• It is a built-in function of Python, used to display output on the
screen.
• Syntax: print(string)
• Example:
Comments
• Single Line Comments
• Multiline Comments
Keywords
• Keywords are reserved words in python that have a specific meaning
and operation.
• There are 35 keywords in python 3. To print all these keywords , run
the following code:
Identifiers
• A random name made out of letters, digits and underscore(_) to identify
a function name, a program name or a memory location is known as an
identifier. Python is a case sensitive language as it treats lower and
upper case letters differently.
Rules for creating identifiers:
• It must start with a letter from A to Z or a to z or an Underscore(_)
• Keywords cannot be used as identifiers
• Python does not allow identifiers containing punctuation characters
such as @,$, and %
• It can be followed by any of the letters, digits(0-9), or underscore.
Valid identifiers:
shapeclass
shape_1
shape_to_db
_var
• Invalid identifiers:
1shape
my var
*varnew
my@var
Variables
• Variables are used for storing data in the memory. The data can be
numbers, text and objects.
• Syntax: variable_name = variable_value
Num=5
Name=“Akash”
print(Num)
print(Name)
5
Akash
Multiple variable assignment
• Assign multiple values to multiple variable:
num, name, age=101, ”Ravi”, 20
print(num)
print(name)
print(age)
• Assign the same value to multiple variables:
x=y=z=“Ravi”
print(x)
print(y)
print(z)
Data Types
Numeric
• Integers: Integers are the whole numbers consisting of + and – sign
without decimal digits.
• Example : a=10 # This is integer value
• Float: Float data type represents floating point numbers with decimal
point.
• Example: Salary=20000.06
• Complex: It is a number that is written in the form of a+bj. Here a and
b represents the real and imaginary parts of a number respectively.
• Example: c=4+5j
Sequence
• A sequence is an ordered collection of elements, indexed by positive
integers.
• String: String is a sequence of characters like alphabets, digits, and
punctuations, used to store and represent text-based information.
• Example: book:”Artificial Intelligence”
• List: List contains the collection of different types of elements which
are separated by comma and enclosed within square brackets[ ]. It is
ordered, changeable(mutable) and allows duplicate elements.
• Example: list =[10,20,30,40]
Tuple: Tuple is similar to list. It contains a group of elements that can be of
different types. The elements in tuple are separated by comma and
enclosed in parentheses(). It is ordered unchangeable(immutable) and
allows duplicate elements. Here, immutable means that once tuple is
created, it does not allow addition of values.
Example: tuple1=(12,5.7, ”10th”)
Sequential
Control
Conditional
Structures
Loop
Conditional Control Structures
Conditional
control
Structures
For x in range(5):
if x==3:
continue
print(x)
Output:
0
1
2
4
for-else loop: The else clause of loop will be
executed only if the loop completes all of its
iterations without being hit by the break statement.
Program: Print the number range from 0-4, and
display a message when the loop has finished.
for x in range(5):
print (x)
else:
print(“Finish loop”)
Output:
0
1
2
3
4
Finish loop
While-else loop: The else clause of the loop will be
executed only when the condition inside the while
loop becomes false.
Print the value of variable until it becomes 0 and
display a message about its completion.
Python Data structure
• List: A list can store a sequence of values belongings to any data
type. Python lists are changeable and allow duplicate members.
Output: Output:
A T
Operations on List
a)Joining Lists: Two lists can be b)Replicating Lists: List can be
added using concatenation replicate as many times as you
operator(+) need, you can use a replication
operator(*).
List1=[1,2,3,4]
List2= [5,6,7,8]
List1=[1,2,3,4]
new_list=List1+List2 #joining both list
new_list=List1*3 #replicating list
print(“New list is:”, new_list)
print(“New list is:”, new_list)
Output:
Output:
New list is: [1,2,3,4,5,6,7,8]
New list is: [1,2,3,4,1,2,3,4,1,2,3,4]
Slicing List: List slicing refers to the process of extracting the
sub part of list.
List1=[10,20,30,40,50,60]
List1.insert(2,70)
print(“Updated list is:”, List1)
Output:
Updated list is: [10,20,70,30,40,50,60]
List Built-in Functions for deleting
elements
• pop():This function is used to remove the desired element from the
list using index value as a parameter.
Note: While using a pop() function, you should remember that if you do not specify
any index value as a parameter, then this function will remove the last element
from the list.
List1=[10,20,30,40,50,60] List1=[10,20,30,40,50,60]
List1.pop( ) # delete last element List1.pop(30) # delete 30 from list
print(“Updated list is:”, List1) print(“Updated list is:”, List1)
Output: Output:
Updated list is: [10,20,30,40,50] Updated list is: [10,20,40,50,60]
• Remove(): This function is mostly • del : del keyword is used to
used to remove the first remove an individual element, a
occurrence of the specified sublist of elements or a whole
element from the list when you list.
do not know the index value.
list1=[10,20,30,40,50,60]
del list1[3]
List1=[10,20,60,30,40,50,60,70,60] print(list1)
List1.remove( 60) # remove element 60
print(“Updated list is:”, List1) Output:[10,20,30,50,60]
Output: list1=[10,20,30,40,50,60]
Updated list is: [10,20,30,40,50,60,70,60] del list1[0:4:2]
print(list1)
Output:[20,40,50,60]