0% found this document useful (0 votes)
5 views50 pages

Python Introduction

The document provides an introduction to Python programming, covering its features, applications, and basic concepts such as variables, data types, control structures, and data structures. It explains the syntax for various operations, including input/output functions, operators, and built-in functions for lists. Additionally, it outlines rules for identifiers, comments, and demonstrates the use of loops and conditional statements.

Uploaded by

geetanjali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views50 pages

Python Introduction

The document provides an introduction to Python programming, covering its features, applications, and basic concepts such as variables, data types, control structures, and data structures. It explains the syntax for various operations, including input/output functions, operators, and built-in functions for lists. Additionally, it outlines rules for identifiers, comments, and demonstrates the use of loops and conditional statements.

Uploaded by

geetanjali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Python Programming

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”)

Sets: Set is an unindexed, unordered collection of values of any type, with


no duplicate entry.
Example: set1={1,2,5,6}

Dictionary: It is a collection of items where each item is a key:value pair.


Example: dict1={“1”:”Wipro”, 2:”Infosys”,3:”Genpect”}
Input in python using input()
function
• Syntax: input([prompt])
name=input(“Enter your name”)
print(name)
Input in python using output()
function
•Syntax: print(expression/constant/variable)
print(10*20-45)
335 Output
Python Operators
• Arithmetic Operators:
• Relational Operators or Comparison Operators:
Logical Operators
Assignment Operators
Type conversion
• Implicit Type conversion: Python automatically converts one data
type into another data type. This process does not need any user
involvement.
• Example
• Explicit Type Conversion: You can convert the data type of an object
to another data type using predefined functions such as int(), float(),
str(),bool(),complex(),tuple() etc.
• This type of conversion is also known as typecasting because the user
casts(changes) the data type of the objects.
Control Structures

Sequential

Control
Conditional
Structures

Loop
Conditional Control Structures

Conditional
control
Structures

if if-else elif else nested if


If Statement
• This statement is used to execute one or more statements depending
on whether a condition is true or not.
Example:
If-else statement
• The if-else statement evaluates the condition and executes body of if
when the condition is true. If the condition is false, body of else is
executed.
• Note: Indentation is used to separate the blocks.
Example:
If-elif-else statement
Nested If Statement
Loop Control
structures

For (Control While(Conditional


Loop) Loop)
For Loop
Range Function: By default starts from value 0 and
self increments by value 1. It always includes the
first value but excludes the last value.
Examples: • Print the numbers from 10 to 1
using 3 parameters.
• Print the first 5 whole numbers for x in range(10,0,-1):
for x in range(5): print(x)
print(x)
Output:
10
Output:
9
0
8
1
7
2
6
3
5
4
4
3
2
1
While Loop: it is used to iterate a set of
statements until the given condition evaluates to
false.
Break Statement: It can be used to
unconditionally jump out of the
loop.
for x in range(100):
if x==3:
break
print(x)
Output:
0
1
2
Continue Statement: It is used to instruct a program
to skip the rest of the statements of the current loop
block and move to the next iteration of the loop.

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.

List = [‘r’ , ’o’ , ’b’ , ’o’ , ’t’]


print(“All elements of given list are:”)
For x in List:
print(x)
Output:
All elements of given list are:
r
o
b
o
t
Access the elements of List

list1=[‘D’, ‘A’, ‘T’, ‘A’] list1=[‘D’, ‘A’, ‘T’, ‘A’]


x= list1[3] x= list1[-2]
print(x) print(x)

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.

#Reversing list with slicing


# Slicing with step value
List1=[10,220,30,40,50,60,70,80] List1=[10,20,30,40,50,60,70,80]
List1=[10,220,30,40,50,60,70,80]
sub_list= List1[2:3] rev_list= List1[::-1]
sub_list= List1[0:4:2]
print(“New list is:”, sub_list) print(“New list is:”, rev_list)
print(“New list is:”, sub_list)
Output: Output:
Output:
New list is: [30] New list is: [80, 70, 60, 50, 40,
New list is: [10, 30]
30,20,10]
List Built-in Functions for Inserting
Elements
• append():This function is used to • extend():This function is used to
add a new element to the end of add multiple elements in an
the list. Only one element can be existing list.
added at a time using append().
List1=[10,20,30,40,50,60]
List1=[10,20,30,40,50,60] List1.extend([70,80,90,100])
List1.append(70) print(“Updated list is:”, List1)
print(“Updated list is:”, List1)
Output:
Output: Updated list is:
Updated list is: [10,20,30,40,50,60,70] [10,20,30,40,50,60,70,80,90,100]
• Insert(): This function is used to insert an element in the existing list
at specified location. Two arguments are used in this function:
i. Index value where you want to insert a new element
ii. The new element itself

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]

You might also like