SlideShare a Scribd company logo
1 of 44
K.LEELARANI AP/CSE
KAMARAJ COLLEGE OF ENGINEERING AND TECHNOLOGY
 What is Python
 History of Python
 Why do people use Python
 Applications of python
 Who uses python today
 Running Python
 Variable in python
 Python Data Types
 Input() function
 Conditional and looping Statements
 Python is a general purpose, high level ,object
oriented programming language.
 Python is also called as Interpreted language
 Invented in the Netherlands, early 90s by Guido
van Rossum •
 Guido Van Rossum is fan of ‘Monty Python’s
Flying Circus’, this is a famous TV show in
Netherlands •
 Named after Monty Python •
 Open sourced from the beginning
 Google makes extensive use of Python in its web
search system.
 Intel, Cisco and IBM use Python for hardware
testing.
 The YouTube video sharing service is largely
written in Python
 Facebook ,instagram…
 Free and Open Source
 Extensible and Embeddable
 High level ,interpreted language
 Large standard libraries to solve common tasks.
 Web Applications
 Scientific and Numeric Computing
> Earthpy
> Astropy
 Simple syntax (interactive mode ,script mode)
 >>> print 'Hello world’
Hello world
>>>2+3
>>> 5
 A Python variable is a reserved memory location
to store values.
 In other words, a variable in a python program
gives data to the computer for processing.
 Every value in Python has a datatype.
 >>> year =2019
>>>type(year)
<class 'int'>
 >>> department=“CSE”
>>>type(department)
<class ‘str’>
Python Data Types
Numeric String List Tuple Dictionary Boolean
 Python has three numeric types
> integer
> floating point numbers
> Complex numbers (a+bi)
Practice:
>>>a=15
>>>type(a)
>>>b=1.5
>>>type(b)
 A string in python can be a series or a sequence
of alphabets, numerals and special characters.
 Single quotes or double quotes are used to
represent strings
 The first character of a string has an index 0.
Practice:
>>>department=“cse”
>>>type(department)
<class ‘str’>
>>> string1=“Hi Friends”
>>>string1
Concatenation Operator (+) – Combine two or
more than two strings
>>> string1 + “How are You”
Repetition Operator (*) - Repeat the same
string several times
>>> string1 *2
 List:
The list is a most versatile Data type available
in Python which can be written as a list of
comma-separated values (items) between square
brackets. Important thing about a list is that
items in a list need not be of the same type.
Example:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
s.no Function with description
1. cmp(list1, list2) #Compares elements of both lists
2. len(list) #Gives the total length of the list
3. max(list) # Returns item from the list with max value.
4. min(list) #Returns item from the list with min value.
Tuples:
A tuple is a sequence of immutable Python
objects. Tuples are sequences, just like lists. The
differences between tuples and lists are, the
tuples cannot be changed unlike lists and tuples
use parentheses, whereas lists use square
brackets.
Example:
tup1 = (1, 2, 3, 4, 5 )
tup2 = ("a", "b", "c", "d“)
SN Function with Description
1 cmp(tuple1, tuple2) #Compares
elements of both tuples.
2 len(tuple) #Gives the total length
of the tuple.
3 max(tuple) #Returns item from the
tuple with max value
4 min(tuple) # Returns item from
the tuple with min value
 Unordered collection of key-value pairs.
Keys and values can be of any type in dictionary
 Items in dictionaries are enclosed in the curly-
braces {},Separated by the comma (,)
 A colon is used to separate key from value
>>>dict1={1:”first”,”second”:2}
>>>dict1[3]=“third line” #add new item
>>>dict1
>>>dict1.keys() # display dictionary keys
>>>dict1.values() # display dictionary values
The True and False data is known as Boolean
Practice:
>>>A=True
>>>type(A)
>>>B=False
>>>type(B)
>>>name = input(“What is your name?”)
>>>print (“Hello”+name)
>>>age=input(“Enter your age?”)
>>>age
>>>hobbies=input(“What are your hobbies?”)
>>>hobbies
 Write a program to print your name, age,and
name of the school.
 Write a program to find the area of a
rectangle/square
 If
 If..else
 Nested if
if test expression:
statement(s)
• Here, the program evaluates the test expression and will
execute statement(s) only if the text expression is True.
• If the text expression is False, the statement(s) is not
executed.
num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.")
Syntax of if...else
if test expression:
Body of if
else:
Body of else
# Program checks if the number is positive or
negative
# And displays an appropriate message
num = 3
if num >0:
print("Positive number")
else:
print("Negative number")
Syntax of if...elif...else
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
# In this program,we check if the number is positive or
negative or zero and display an appropriate message
num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
 Looping is defined as repeatedly executing block of
statements for certain number of times.
 Python has mainly 2 types of looping statements
> for loop
> while loop
 The for loop in Python is used to iterate over a
sequence (list, tuple, string) or other iterable objects.
Iterating over a sequence is called traversal.
for val in sequence:
Body of for
• Here, val is the variable that takes the value of the item
inside the sequence on each iteration.
• Loop continues until we reach the last item in the sequence.
The body of for loop is separated from the rest of the code
using indentation.
# Program to find the sum of all numbers
stored in a list
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
print("The sum is", sum)
 A for loop can have an optional else block as well.
The else part is executed if the items in the sequence
used in for loop exhausts.
 A for loop's else part runs if no break occurs.
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
The while loop in Python is used to iterate over a block of
code as long as the test expression (condition) is true.
while test_expression:
Body of while
• In while loop, test expression is checked first. The body of the loop
is entered only if the test_expression evaluates to True. After one
iteration, the test expression is checked again. This process
continues until the test_expression evaluates to False.
• In Python, the body of the while loop is determined through
indentation.
• Body starts with indentation and the first unindented line marks the
end.
# Program to add natural numbers upto sum = 1+2+3+...+n
# To take input from the user,
# n = int(input("Enter n: "))
n = 10
# initialize sum and counter
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1 # update counter
# print the sum
print("The sum is", sum)
THANK YOU

More Related Content

What's hot (20)

Python programming language
Python programming languagePython programming language
Python programming language
 
Python Basics
Python Basics Python Basics
Python Basics
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
 
A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with Python
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
Python
PythonPython
Python
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Python advance
Python advancePython advance
Python advance
 
Python
PythonPython
Python
 
Python made easy
Python made easy Python made easy
Python made easy
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Python ppt
Python pptPython ppt
Python ppt
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
 
python codes
python codespython codes
python codes
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
 

Similar to Python introduction

Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on SessionDharmesh Tank
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : PythonRaghu Kumar
 
Python lab basics
Python lab basicsPython lab basics
Python lab basicsAbi_Kasi
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxpriestmanmable
 
manish python.pptx
manish python.pptxmanish python.pptx
manish python.pptxssuser92d141
 

Similar to Python introduction (20)

Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
Python programming
Python  programmingPython  programming
Python programming
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Pythonintro
PythonintroPythonintro
Pythonintro
 
Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on Session
 
Day2
Day2Day2
Day2
 
Python basics
Python basicsPython basics
Python basics
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
Python lab basics
Python lab basicsPython lab basics
Python lab basics
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Python slide.1
Python slide.1Python slide.1
Python slide.1
 
manish python.pptx
manish python.pptxmanish python.pptx
manish python.pptx
 

Recently uploaded

Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfJNTUA
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)NareenAsad
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2T.D. Shashikala
 
Circuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringCircuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringKanchhaTamang
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdfKamal Acharya
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringC Sai Kiran
 
Electrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineElectrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineJulioCesarSalazarHer1
 
ANSI(ST)-III_Manufacturing-I_05052020.pdf
ANSI(ST)-III_Manufacturing-I_05052020.pdfANSI(ST)-III_Manufacturing-I_05052020.pdf
ANSI(ST)-III_Manufacturing-I_05052020.pdfBertinKamsipa1
 
Teachers record management system project report..pdf
Teachers record management system project report..pdfTeachers record management system project report..pdf
Teachers record management system project report..pdfKamal Acharya
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfMadan Karki
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5T.D. Shashikala
 
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...ShivamTiwari995432
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...MohammadAliNayeem
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGKOUSTAV SARKAR
 
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Lovely Professional University
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...Roi Lipman
 
Quiz application system project report..pdf
Quiz application system project report..pdfQuiz application system project report..pdf
Quiz application system project report..pdfKamal Acharya
 
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDrGurudutt
 
"United Nations Park" Site Visit Report.
"United Nations Park" Site  Visit Report."United Nations Park" Site  Visit Report.
"United Nations Park" Site Visit Report.MdManikurRahman
 

Recently uploaded (20)

Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
 
Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
Circuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringCircuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineering
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdf
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
Electrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineElectrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission line
 
ANSI(ST)-III_Manufacturing-I_05052020.pdf
ANSI(ST)-III_Manufacturing-I_05052020.pdfANSI(ST)-III_Manufacturing-I_05052020.pdf
ANSI(ST)-III_Manufacturing-I_05052020.pdf
 
Teachers record management system project report..pdf
Teachers record management system project report..pdfTeachers record management system project report..pdf
Teachers record management system project report..pdf
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
 
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
 
Quiz application system project report..pdf
Quiz application system project report..pdfQuiz application system project report..pdf
Quiz application system project report..pdf
 
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
 
"United Nations Park" Site Visit Report.
"United Nations Park" Site  Visit Report."United Nations Park" Site  Visit Report.
"United Nations Park" Site Visit Report.
 

Python introduction

  • 1. K.LEELARANI AP/CSE KAMARAJ COLLEGE OF ENGINEERING AND TECHNOLOGY
  • 2.  What is Python  History of Python  Why do people use Python  Applications of python  Who uses python today  Running Python  Variable in python  Python Data Types  Input() function  Conditional and looping Statements
  • 3.  Python is a general purpose, high level ,object oriented programming language.  Python is also called as Interpreted language
  • 4.  Invented in the Netherlands, early 90s by Guido van Rossum •  Guido Van Rossum is fan of ‘Monty Python’s Flying Circus’, this is a famous TV show in Netherlands •  Named after Monty Python •  Open sourced from the beginning
  • 5.  Google makes extensive use of Python in its web search system.  Intel, Cisco and IBM use Python for hardware testing.  The YouTube video sharing service is largely written in Python  Facebook ,instagram…
  • 6.  Free and Open Source  Extensible and Embeddable  High level ,interpreted language  Large standard libraries to solve common tasks.
  • 7.  Web Applications  Scientific and Numeric Computing > Earthpy > Astropy
  • 8.  Simple syntax (interactive mode ,script mode)  >>> print 'Hello world’ Hello world >>>2+3 >>> 5
  • 9.  A Python variable is a reserved memory location to store values.  In other words, a variable in a python program gives data to the computer for processing.  Every value in Python has a datatype.
  • 10.  >>> year =2019 >>>type(year) <class 'int'>  >>> department=“CSE” >>>type(department) <class ‘str’>
  • 11. Python Data Types Numeric String List Tuple Dictionary Boolean
  • 12.  Python has three numeric types > integer > floating point numbers > Complex numbers (a+bi) Practice: >>>a=15 >>>type(a) >>>b=1.5 >>>type(b)
  • 13.  A string in python can be a series or a sequence of alphabets, numerals and special characters.  Single quotes or double quotes are used to represent strings  The first character of a string has an index 0. Practice: >>>department=“cse” >>>type(department) <class ‘str’>
  • 14. >>> string1=“Hi Friends” >>>string1 Concatenation Operator (+) – Combine two or more than two strings >>> string1 + “How are You” Repetition Operator (*) - Repeat the same string several times >>> string1 *2
  • 15.  List: The list is a most versatile Data type available in Python which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that items in a list need not be of the same type. Example: list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ];
  • 16. s.no Function with description 1. cmp(list1, list2) #Compares elements of both lists 2. len(list) #Gives the total length of the list 3. max(list) # Returns item from the list with max value. 4. min(list) #Returns item from the list with min value.
  • 17. Tuples: A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. Example: tup1 = (1, 2, 3, 4, 5 ) tup2 = ("a", "b", "c", "d“)
  • 18. SN Function with Description 1 cmp(tuple1, tuple2) #Compares elements of both tuples. 2 len(tuple) #Gives the total length of the tuple. 3 max(tuple) #Returns item from the tuple with max value 4 min(tuple) # Returns item from the tuple with min value
  • 19.  Unordered collection of key-value pairs. Keys and values can be of any type in dictionary  Items in dictionaries are enclosed in the curly- braces {},Separated by the comma (,)  A colon is used to separate key from value
  • 20. >>>dict1={1:”first”,”second”:2} >>>dict1[3]=“third line” #add new item >>>dict1 >>>dict1.keys() # display dictionary keys >>>dict1.values() # display dictionary values
  • 21. The True and False data is known as Boolean Practice: >>>A=True >>>type(A) >>>B=False >>>type(B)
  • 22. >>>name = input(“What is your name?”) >>>print (“Hello”+name) >>>age=input(“Enter your age?”) >>>age >>>hobbies=input(“What are your hobbies?”) >>>hobbies
  • 23.  Write a program to print your name, age,and name of the school.  Write a program to find the area of a rectangle/square
  • 24.
  • 26.
  • 27. if test expression: statement(s) • Here, the program evaluates the test expression and will execute statement(s) only if the text expression is True. • If the text expression is False, the statement(s) is not executed.
  • 28. num = 3 if num > 0: print(num, "is a positive number.") print("This is always printed.") num = -1 if num > 0: print(num, "is a positive number.")
  • 29.
  • 30. Syntax of if...else if test expression: Body of if else: Body of else
  • 31. # Program checks if the number is positive or negative # And displays an appropriate message num = 3 if num >0: print("Positive number") else: print("Negative number")
  • 32.
  • 33. Syntax of if...elif...else if test expression: Body of if elif test expression: Body of elif else: Body of else
  • 34. # In this program,we check if the number is positive or negative or zero and display an appropriate message num = 3.4 if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")
  • 35. num = float(input("Enter a number: ")) if num >= 0: if num == 0: print("Zero") else: print("Positive number") else: print("Negative number")
  • 36.  Looping is defined as repeatedly executing block of statements for certain number of times.  Python has mainly 2 types of looping statements > for loop > while loop  The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal.
  • 37. for val in sequence: Body of for • Here, val is the variable that takes the value of the item inside the sequence on each iteration. • Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.
  • 38. # Program to find the sum of all numbers stored in a list # List of numbers numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11] # variable to store the sum sum = 0 # iterate over the list for val in numbers: sum = sum+val print("The sum is", sum)
  • 39.  A for loop can have an optional else block as well. The else part is executed if the items in the sequence used in for loop exhausts.  A for loop's else part runs if no break occurs.
  • 40. digits = [0, 1, 5] for i in digits: print(i) else: print("No items left.")
  • 41. The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
  • 42. while test_expression: Body of while • In while loop, test expression is checked first. The body of the loop is entered only if the test_expression evaluates to True. After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False. • In Python, the body of the while loop is determined through indentation. • Body starts with indentation and the first unindented line marks the end.
  • 43. # Program to add natural numbers upto sum = 1+2+3+...+n # To take input from the user, # n = int(input("Enter n: ")) n = 10 # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum + i i = i+1 # update counter # print the sum print("The sum is", sum)