Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

INTRODUCTION TO

PYTHON PROGRAMMING
WHY PYTHON?
MULTIPURPOSE (Web , GUI, Scripting etc.)
Object Oriented
Interpreted
Strongly Typed and Dynamically Typed
Focus on Readability and Programmability
SYNTAX
INDENTATION

 Most languages like C, C++ , JAVA etc. don’t care


about indexing. Curly braces are used to separate the
blocks of code.
 Indentations make the code more readable , separates
and groups the different blocks of the code.
Sample Code
CONTROL STATEMENTS
 There are two different types of control statements which change the
normal execution of the program.
 Conditional Statements
 Iteration Statements
 In control statements, indentation plays a major role.
Conditional Statements
 Conditional Statements are used to execute a particular
block of statements on the basis of the result of a
specified condition.
 Arithmetic and Logical Operators can be used to create
conditions.
 For eg., Assigning the grades of a student on the basis of
the marks scored, checking if a person is above 18 years
and thus check his eligibility to vote etc.
ITERATION STATEMENTS
 The iteration statements are used to repeat a specified
block of code multiple times , on the basis of the result of
a given condition.
 The three most widely used iteration statements or loops
in Python are:
For Loop
While Loop
Strings in Python
Strings refers to a collection of characters .
It is specified within double or single quotes.
Strings in python are immutable.
However, Python provides us with several inbuilt
functions to manipulate strings and get the
desired results.
 STRING CONCATENATION: Joining two are more different strings.

 STRING INDEXES:

 STRING SLICING: It is used to retrieve a slice/part if the string.


 find(): It is used to find the first instance of a substring in
the given string. If no instance is found -1 is returned.

 isupper(): It is used to check whether all the characters


of the string are uppercase or not.

 len(): It is used to find the length of the given string.


DATA STRUCTURES IN PYTHON

There are some in-built functions in python which


can be used for storing and retrieving data.
These are:
Lists
Dictionaries
Sets
Tuples
LISTS
 A list in python is used to store a collection of related
data.
 For eg., If we need to store the ages of a group of
students, instead of storing the in separate variables, we
can store them in a list.
 All the elements stored in a list can be of different data
types.
For e.g, l =[‘vit’,10,’10.98’] is a valid list in python
LIST MANIPULATION
 Some of the in-built methods to manipulate strings are:
append(): Add an element of the end list.
count(): Returns the count of the number of items passed as an argument.
remove(): Removes a particular element in a group.
sort(): Sorts the elements of the list in ascending order.
extend(): This method adds the elements of on list to other.
insert(): Insert an element at a specific index in the list.
pop(): Deletes a particular element in the list. If no element is specified,
the last element is deleted. In addition to deletion, the specified
element is printed by this method
DICTIONARIES
 A dictionary is collection of related data PAIRS.
 The general syntax is:
dictionaryName: {dictionaryKey: data}
the same dictionary key cannot be reused.
 A dictionary can also be declared using the dict()
function.
 Dictionaries like lists are mutable.
DICTIONARIES MANIPULATION
 Adding elements in the dictionary:
Items can be added in the dictionary using the following syntax:
dictionaryName[‘newKey’] = “value”
 Removing/Deleting elements in the dictionary:
Items can be deleted as follows:
del dictonaryName[‘dictionaryKey’]
SETS
Sets are like lists except for the fact that
they store only unique elements and
eliminate the duplicates.
A list can be converted into set to remove
duplicate elements.
Frozensets are immutable counterparts od
sets.
TUPLE
Tuples are immutable lists.
Unlike lists, the tuples are enclosed within
round parenthesis.
Tuples are thought of as immutable lists.
FUNCTIONS
 Python has flexibility to define and can use them in the program.
 This prevents data redundancy and improves the structure of our code ,
making it more readable .
 The syntax for defining our own functions in python is :
def functionName(parameters):
//code for what the function should do
return [expression]
 The function might or might not accept parameters
 The functions can be called anytime in the code using :
functinName([parameters])
LAMBDA FUNCTIONS
 Lambda functions are used to create small , one-time and anonymous
function objects in python.
 It can have any number of parameters.
 For e,g,
COMPARISON IN CPP AND PYTHON CODE
(PROGRAM TO REVERSE A STRING)

PYTHON

CPP
APPLICATIONS OF PYTHON

You might also like