Python

You might also like

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

PYTHON

PYTHON INTRODUCTION

• Python created by Guido Van Rossum ,released in 1991


• It is high level language
• Python programs are easy to understand
• Python programs are executed by interpreter.
• It can run on various OS,hardware platform.
• It is also helpful in Web Development
WHAT IS DATA TYPES?

• Data types can be used to store a collection of items.


DATA TYPES

ORDERED COLLECTION UNORDERED COLLECTION


(IT MAINTAINS THE ORDER OF ELEMENTS (ORDER OF ELEMENTS NOT MAINTAINED)
UNTIL WE CHANGE IT)
1. STRING 1. SET
2. LIST
3. TUPLE
DIFFERENCE BETWEEN LIST AND TUPLE
• LIST can store multiple items in a single variable ,like
a=[1,2,3,4,5]
• TUPLE is enclosed in parentheses ( ),
• LIST is enclosed in square brackets[ ], elements elements separated with commas
separated with commas.

• TUPLE IS IMMUTABLE (means length


• LIST IS MUTABLE(means length can be changed cannot be changed and elements cannot be
and elements can be sustitued)
sustitued)

• List items are ordered , changeable and allow


duplicate values. • Tuple allows duplicate values.

• List items are indexed( the first item has index [0], the
second item has index [1] etc.
LIST
• List support operation like concatenation using + operator.
• List items can be accessed by indexing- postive, negative indexing.
• List can be sliced.
• List can be mutable (ie, It is possible to change their content)
• In List we can add new item at the end by using append () method
• In list we can remove an item using del statement.
LIST – WE USE [ ] BRACKET

• >> > a= [1,2,3]


• >>> b=[‘red’, ‘yellow’ , ‘brown’, ‘pink’, ‘rose’ , ‘blue’]
• >>>print (a)
• [1,2,3]
• >>> print (b)
• [‘red’, ‘yellow’ , ‘brown’, ‘pink’, ‘rose’ , ‘blue’]
TUPLE-WE USE ( ) BRACKET

• >> > a= (1,2,3)


• >>> b=(‘red’, ‘yellow’ , ‘brown’, ‘pink’, ‘rose’ , ‘blue’)
• >>>print (a)
• (1,2,3)
• >>> print (b)
• (‘red’, ‘yellow’ , ‘brown’ ,‘pink’, ‘rose’ , ‘blue’)
• Set is an undordered collection of items
SET • Seperated by commas
• Items are enclosed in curly { } brackets.
• Set cannot have duplicate values,
• items(elements ) cannot be changed

>> > a= {1,2,3}


>>> b={‘red’, ‘yellow’ , ‘brown’, ‘pink’, ‘rose’ , ‘blue’}
>>>print (a)
{1,2,3}
>>> print (b)
{‘red’, ‘yellow’ , ‘brown’, ‘pink’, ‘rose’ , ‘blue’}
SET CANNOT HAVE DUPLICATE VALUES,

• >> > a= {1,3,2,3,3}


• >>>print (a)
• {1,2,3}
PYTHON COMPARISON OPERATORS
It is used to compare 2 values

OPERATOR NAME EXAMPLE RESULT

== EQUAL A==B
>>> A= 10
!= NOT EQUAL A!=B
>>>B=5
> GRAETER A>B
THAN >>>PRINT (A==B)
< LESS THAN A<B TRUE OR
FALSE
FALSE
>= GREATER A>=B
THAN OR
EQUAL TO >>> PRINT (A!=B)
<= LESS THAN A<=B
OR EQUAL
TRUE
TO
PYTHON LOGICAL OPERATORS
• It is used to combine conditional statements..The value is either True or False

>>> a=7
>>>b=12
>>> print (a<5 and b>10)
False
>>> print (a<5 or b>10)
LOGICAL True
NOT OPERATO AND >>>print(not (a<5 and b<10))
R
True

OR
IDENTITY OPERATORS

• It is used to compare the memory location of 2 objects.


• 2 types of identity operator
Is ( returns true if both variable point same object.)
Is not( returns false if both variable point same object.)
MEMBERSHIP OPERATORS

• 2 types
In( returns true if a sequence value is present in the object.)

Not in ( returns true if a sequence value is not present in the object.)


WHILE LOOP

• It is used to execute a block of satements repeatedly until a given condition is


satisfied.
To print hello 5 times
Count = 0
Syntax Whlie count<5
Print(“hello”)
While expression: Count = count+1
Output
statement (s) Hello
Hello
Hello
Hello
hello
LOOP CONTROL STATEMENTS

• 2 types
• Break continue
If you want to terminate a loop and skip to The continue statement returns the
next code after the loop break statement will control to the beginning of the while loop
hel
DECISION MAKING STATEMENT-

3 types of decision making


An “if statement” is written by using the if keyword.

• If statement
-it is used to test a particular condition
SYNTAX
If condition:
print statement
IF-ELSE STATEMENT

-it helps to check 2 conditions “true “ else “false”

SYNTAX
If condition:
print statement 1
else:
print statement 2
ELIF STATEMENT
• - it helps to check multiple condtion.

SYNTAX
if condition 1:
block of statements 1
elif condition 2:
block of statements 2
elif condition 3:
block of statements 3
else :
block statement 4
PYTHON FUNCTIONS
• The function means a bock of code define with a name.
• If we need to perform the same task multiple times without writing the same
code again we use function.
• We can create our own functions (user-define functions.)
example: greet(), sum() , total()
• There are built-in functions also in python like
print() , input() , range ()

IN PYTHON A FUNCTION IS DEFINED USING THE def KEYWORD

def greet(), def sum(a,b)


I .CREATING A FUNCTION
def greet():
Print (“good morning”)

II.CALLING A FUNCTION

To call a function, use the function name followed by parenthesis.


EXAMPLE
def greet():
Print (“good morning”)
greet()
CREATING A FUNCTION WITH PARAMETERS

• Example
def greet(name):
print (“good morning” +name)
a =input(“Enter your name”)
greet(a)
CREATING A FUNCTION WITH PARAMETERS
AND RETURN VALUE
• Function can return a value.
• The return value is the output of the function.
• We want to the keyword return to return from a function.
• EXAMPLE
def sum(a,b):
Output
c= a+b Enter 1 number:
return c Enter 2 number:
Sum of number=
X= int (input (“Enter 1 number: “))
Y= int (input (“Enter 2 number: “))
print(“sum of above numbers=“,sum(x,y))

You might also like