SlideShare a Scribd company logo
1 of 24
BS (Hons) I.T (Morning)
1st Semester
M.Hamza Riaz
University Of Education,Lahore.(Okara Campus)
University Of Education.Okara Campus
University Of Education.Okara Campus
 A programming language with strong
similarities to PERL, but with powerful
typing and object oriented features.
› Commonly used for producing HTML content
on websites. Great for text files.
› Useful built-in types (lists, dictionaries).
› Clean syntax, powerful extensions.
University Of Education.Okara Campus
University Of Education.Okara Campus
Introduction To C.S & Python
 Python is a widely used general-
purpose, high-level programming
language.Its design philosophy emphasizes
code readability, and its syntax allows
programmers to express concepts in
fewer lines of code than would be possible
in languages such as C++ or Java.The
language provides constructs intended to
enable clear programs on both a small and
large scale.
 To understand the string data type and
how strings are represented in the
computer.
 To be familiar with various operations
that can be performed on strings
through built-in functions and the string
library.
University Of Education.Okara Campus
Introduction To C.S & Python
 The most common use of personal
computers is word processing.
 Text is represented in programs by the
string data type.
 A string is a sequence of characters
enclosed within quotation marks (") or
apostrophes (').
University Of Education.Okara Campus
Introduction To C.S & Python
>>> str1="Hello"
>>> str2='spam'
>>> print(str1, str2)
Hello spam
>>> type(str1)
<class 'str'>
>>> type(str2)
<class 'str'>
University Of Education.Okara Campus
Introduction To C.S & Python
 Getting a string as input
>>> firstName = input("Please enter your name: ")
Please enter your name: John
>>> print("Hello", firstName)
Hello John
 Notice that the input is not evaluated. We
want to store the typed characters, not to
evaluate them as a Python expression.
University Of Education.Okara Campus
Introduction To C.S & Python
 We can access the individual
characters in a string through indexing.
 The positions in a string are numbered
from the left, starting with 0.
 The general form is <string>[<expr>],
where the value of expr determines
which character is selected from the
string.
University Of Education.Okara Campus
Introduction To C.S & Python
>>> greet = "Hello Bob"
>>> greet[0]
'H'
>>> print(greet[0], greet[2], greet[4])
H l o
>>> x = 8
>>> print(greet[x - 2])
B
University Of Education.Okara Campus
Introduction To C.S & Python
H e l l o B o b
0 1 2 3 4 5 6 7 8
University Of Education.Okara Campus
Introduction To C.S & Python
Operator Meaning
+ Concatenation
* Repetition
<string>[] Indexing
<string>[:] Slicing
len(<string>) Length
for <var> in <string> Iteration through characters
 Usernames on a computer system
› First initial, first seven characters of last name
# get user’s first and last names
first = input("Please enter your first name (all lowercase): ")
last = input("Please enter your last name (all lowercase): ")
# concatenate first initial with 7 chars of last name
uname = first[0] + last[:7]
University Of Education.Okara Campus
Introduction To C.S & Python
>>> Please enter your first name (all lowercase):
john
Please enter your last name (all lowercase): doe
uname = jdoe
>>>
Please enter your first name (all lowercase): donna
Please enter your last name (all lowercase):
rostenkowski
uname = drostenk
University Of Education.Okara Campus
Introduction To C.S & Python
 Another use – converting an int that
stands for the month into the three letter
abbreviation for that month.
 Store all the names in one big string:
“JanFebMarAprMayJunJulAugSepOctNovDec”
 Use the month number as an index for
slicing this string:
monthAbbrev = months[pos:pos+3]
University Of Education.Okara Campus
Introduction To C.S & Python
University Of Education.Okara Campus
Introduction To C.S & Python
Month Number Position
Jan 1 0
Feb 2 3
Mar 3 6
Apr 4 9
 To get the correct position, subtract one from the
month number and multiply by three
# month.py
# A program to print the abbreviation of a month, given its number
def main():
# months is used as a lookup table
months = "JanFebMarAprMayJunJulAugSepOctNovDec"
n = eval(input("Enter a month number (1-12): "))
# compute starting position of month n in months
pos = (n-1) * 3
# Grab the appropriate slice from months
monthAbbrev = months[pos:pos+3]
# print the result
print ("The month abbreviation is", monthAbbrev + ".")
University Of Education.Okara Campus
Introduction To C.S & Python
>>> main()
Enter a month number (1-12): 1
The month abbreviation is Jan.
>>> main()
Enter a month number (1-12): 12
The month abbreviation is Dec.
One weakness – this method only
works where the potential
outputs all have the same
length.
University Of Education.Okara Campus
Introduction To C.S & Python
 Strings are always sequences of
characters, but lists can be sequences of
arbitrary values.
 Lists can have numbers, strings, or both!
myList = [1, "Spam ", 4, "U"]
University Of Education.Okara Campus
Introduction To C.S & Python
 We can use the idea of a list to make
our previous month program even
simpler!
 We change the lookup table for months
to a list:
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"]
University Of Education.Okara Campus
Introduction To C.S & Python
 To get the months out of the sequence,
do this:
monthAbbrev = months[n-1]
Rather than this:
monthAbbrev = months[pos:pos+3]
University Of Education.Okara Campus
Introduction To C.S & Python
# month2.py
# A program to print the month name, given it's number.
# This version uses a list as a lookup table.
def main():
# months is a list used as a lookup table
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
n = eval(input("Enter a month number (1-12): "))
print ("The month abbreviation is", months[n-1] + ".")
main()
 Since the list is indexed starting from 0, the n-1
calculation is straight-forward enough to put in
the print statement without needing a
separate step.
University Of Education.Okara Campus
Introduction To C.S & Python
 This version of the program is easy to
extend to print out the whole month
name rather than an abbreviation!
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"]
University Of Education.Okara Campus
Introduction To C.S & Python

 #include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int ict, isl, eng, math, basic, avg;
cout<<"Enter the Marks of 5 subjects=";
cin>>ict>>isl>>eng>>math>>basic;
avg=(ict+isl+eng+math+basic)/5;
if(avg>=80 && avg<=100)
cout<<"4 GPA";
else if (avg>=70 && avg<80)
cout<<"3 GPA";
else if (avg>=60 && avg <70)
cout<<"2 GPA";
else if (avg>=50 && avg < 60)
cout<<"1 GPA";
else
cout<<"your are fail ";
 getche();
}
University Of Education.Okara Campus
Introduction To C.S & Python
1. Computer Science 9-10
2. WikiPedia
3. www.onlineitlearners.tk
4. www.python.org
5. www.csc.villanova.edu/~map/nlp/pyth
on1
6. Udemy Online course
University Of Education.Okara Campus

More Related Content

Viewers also liked

El barroco
El barrocoEl barroco
El barrocoMiya Kim
 
Crumble de capuccino con helado y gruet de cacao
Crumble de capuccino con helado y gruet de cacaoCrumble de capuccino con helado y gruet de cacao
Crumble de capuccino con helado y gruet de cacaoTrades
 
Presentazione Acof
Presentazione AcofPresentazione Acof
Presentazione Acofangeloalab
 
Genealogy of chandeliers
Genealogy of chandeliersGenealogy of chandeliers
Genealogy of chandeliersFalqoni
 
Objetos de aprendizaje
Objetos de aprendizajeObjetos de aprendizaje
Objetos de aprendizajefernandagtzglz
 
สมุนกะเพรา
สมุนกะเพราสมุนกะเพรา
สมุนกะเพราGuenu Nam
 
Kuzey Anadolu Fayının İzinde Yürüyüş
Kuzey Anadolu Fayının İzinde Yürüyüş Kuzey Anadolu Fayının İzinde Yürüyüş
Kuzey Anadolu Fayının İzinde Yürüyüş Ali Osman Öncel
 
คะแนนม.3 ปี53 3 5
คะแนนม.3 ปี53 3 5คะแนนม.3 ปี53 3 5
คะแนนม.3 ปี53 3 5Aj Yawwalak
 
10G over Copper by 3M
10G over Copper by 3M10G over Copper by 3M
10G over Copper by 3MSheikh Sabri
 

Viewers also liked (17)

El barroco
El barrocoEl barroco
El barroco
 
Pics art 1416335159365
Pics art 1416335159365Pics art 1416335159365
Pics art 1416335159365
 
Crumble de capuccino con helado y gruet de cacao
Crumble de capuccino con helado y gruet de cacaoCrumble de capuccino con helado y gruet de cacao
Crumble de capuccino con helado y gruet de cacao
 
Teatro de sombras
Teatro de sombrasTeatro de sombras
Teatro de sombras
 
El automovilismo
El automovilismo El automovilismo
El automovilismo
 
Presentazione Acof
Presentazione AcofPresentazione Acof
Presentazione Acof
 
Genealogy of chandeliers
Genealogy of chandeliersGenealogy of chandeliers
Genealogy of chandeliers
 
Objetos de aprendizaje
Objetos de aprendizajeObjetos de aprendizaje
Objetos de aprendizaje
 
สมุนกะเพรา
สมุนกะเพราสมุนกะเพรา
สมุนกะเพรา
 
Yesenia
YeseniaYesenia
Yesenia
 
Recogida papel y cartón Coslada
Recogida papel y cartón CosladaRecogida papel y cartón Coslada
Recogida papel y cartón Coslada
 
Las Bits
Las BitsLas Bits
Las Bits
 
Kuzey Anadolu Fayının İzinde Yürüyüş
Kuzey Anadolu Fayının İzinde Yürüyüş Kuzey Anadolu Fayının İzinde Yürüyüş
Kuzey Anadolu Fayının İzinde Yürüyüş
 
It322 intro
It322 introIt322 intro
It322 intro
 
Lch full 0009
Lch full 0009Lch full 0009
Lch full 0009
 
คะแนนม.3 ปี53 3 5
คะแนนม.3 ปี53 3 5คะแนนม.3 ปี53 3 5
คะแนนม.3 ปี53 3 5
 
10G over Copper by 3M
10G over Copper by 3M10G over Copper by 3M
10G over Copper by 3M
 

Similar to Introduction to Python (20)

Sequences: Strings, Lists, and Files
Sequences: Strings, Lists, and FilesSequences: Strings, Lists, and Files
Sequences: Strings, Lists, and Files
 
Chapter05
Chapter05Chapter05
Chapter05
 
R
RR
R
 
Lk module3
Lk module3Lk module3
Lk module3
 
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxCS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
 
Chapter05.ppt
Chapter05.pptChapter05.ppt
Chapter05.ppt
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
R Basics
R BasicsR Basics
R Basics
 
Array and string
Array and stringArray and string
Array and string
 
Python slide.1
Python slide.1Python slide.1
Python slide.1
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016)
 
Python - Data Collection
Python - Data CollectionPython - Data Collection
Python - Data Collection
 
Python :variable types
Python :variable typesPython :variable types
Python :variable types
 
Python introduction
Python introductionPython introduction
Python introduction
 
A short tutorial on r
A short tutorial on rA short tutorial on r
A short tutorial on r
 
Pytho dictionaries
Pytho dictionaries Pytho dictionaries
Pytho dictionaries
 
Unit - 1.ppt
Unit - 1.pptUnit - 1.ppt
Unit - 1.ppt
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptxUnit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptx
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 

More from university of education,Lahore

More from university of education,Lahore (20)

Activites and Time Planning
 Activites and Time Planning Activites and Time Planning
Activites and Time Planning
 
Steganography
SteganographySteganography
Steganography
 
Classical Encryption Techniques
Classical Encryption TechniquesClassical Encryption Techniques
Classical Encryption Techniques
 
Activites and Time Planning
Activites and Time PlanningActivites and Time Planning
Activites and Time Planning
 
OSI Security Architecture
OSI Security ArchitectureOSI Security Architecture
OSI Security Architecture
 
Network Security Terminologies
Network Security TerminologiesNetwork Security Terminologies
Network Security Terminologies
 
Project Scheduling, Planning and Risk Management
Project Scheduling, Planning and Risk ManagementProject Scheduling, Planning and Risk Management
Project Scheduling, Planning and Risk Management
 
Software Testing and Debugging
Software Testing and DebuggingSoftware Testing and Debugging
Software Testing and Debugging
 
ePayment Methods
ePayment MethodsePayment Methods
ePayment Methods
 
SEO
SEOSEO
SEO
 
A Star Search
A Star SearchA Star Search
A Star Search
 
Enterprise Application Integration
Enterprise Application IntegrationEnterprise Application Integration
Enterprise Application Integration
 
Uml Diagrams
Uml DiagramsUml Diagrams
Uml Diagrams
 
eDras Max
eDras MaxeDras Max
eDras Max
 
RAD Model
RAD ModelRAD Model
RAD Model
 
Microsoft Project
Microsoft ProjectMicrosoft Project
Microsoft Project
 
Itertaive Process Development
Itertaive Process DevelopmentItertaive Process Development
Itertaive Process Development
 
Computer Aided Software Engineering Nayab Awan
Computer Aided Software Engineering Nayab AwanComputer Aided Software Engineering Nayab Awan
Computer Aided Software Engineering Nayab Awan
 
Lect 2 assessing the technology landscape
Lect 2 assessing the technology landscapeLect 2 assessing the technology landscape
Lect 2 assessing the technology landscape
 
system level requirements gathering and analysis
system level requirements gathering and analysissystem level requirements gathering and analysis
system level requirements gathering and analysis
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Introduction to Python

  • 1. BS (Hons) I.T (Morning) 1st Semester M.Hamza Riaz University Of Education,Lahore.(Okara Campus) University Of Education.Okara Campus
  • 3.  A programming language with strong similarities to PERL, but with powerful typing and object oriented features. › Commonly used for producing HTML content on websites. Great for text files. › Useful built-in types (lists, dictionaries). › Clean syntax, powerful extensions. University Of Education.Okara Campus
  • 4. University Of Education.Okara Campus Introduction To C.S & Python  Python is a widely used general- purpose, high-level programming language.Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java.The language provides constructs intended to enable clear programs on both a small and large scale.
  • 5.  To understand the string data type and how strings are represented in the computer.  To be familiar with various operations that can be performed on strings through built-in functions and the string library. University Of Education.Okara Campus Introduction To C.S & Python
  • 6.  The most common use of personal computers is word processing.  Text is represented in programs by the string data type.  A string is a sequence of characters enclosed within quotation marks (") or apostrophes ('). University Of Education.Okara Campus Introduction To C.S & Python
  • 7. >>> str1="Hello" >>> str2='spam' >>> print(str1, str2) Hello spam >>> type(str1) <class 'str'> >>> type(str2) <class 'str'> University Of Education.Okara Campus Introduction To C.S & Python
  • 8.  Getting a string as input >>> firstName = input("Please enter your name: ") Please enter your name: John >>> print("Hello", firstName) Hello John  Notice that the input is not evaluated. We want to store the typed characters, not to evaluate them as a Python expression. University Of Education.Okara Campus Introduction To C.S & Python
  • 9.  We can access the individual characters in a string through indexing.  The positions in a string are numbered from the left, starting with 0.  The general form is <string>[<expr>], where the value of expr determines which character is selected from the string. University Of Education.Okara Campus Introduction To C.S & Python
  • 10. >>> greet = "Hello Bob" >>> greet[0] 'H' >>> print(greet[0], greet[2], greet[4]) H l o >>> x = 8 >>> print(greet[x - 2]) B University Of Education.Okara Campus Introduction To C.S & Python H e l l o B o b 0 1 2 3 4 5 6 7 8
  • 11. University Of Education.Okara Campus Introduction To C.S & Python Operator Meaning + Concatenation * Repetition <string>[] Indexing <string>[:] Slicing len(<string>) Length for <var> in <string> Iteration through characters
  • 12.  Usernames on a computer system › First initial, first seven characters of last name # get user’s first and last names first = input("Please enter your first name (all lowercase): ") last = input("Please enter your last name (all lowercase): ") # concatenate first initial with 7 chars of last name uname = first[0] + last[:7] University Of Education.Okara Campus Introduction To C.S & Python
  • 13. >>> Please enter your first name (all lowercase): john Please enter your last name (all lowercase): doe uname = jdoe >>> Please enter your first name (all lowercase): donna Please enter your last name (all lowercase): rostenkowski uname = drostenk University Of Education.Okara Campus Introduction To C.S & Python
  • 14.  Another use – converting an int that stands for the month into the three letter abbreviation for that month.  Store all the names in one big string: “JanFebMarAprMayJunJulAugSepOctNovDec”  Use the month number as an index for slicing this string: monthAbbrev = months[pos:pos+3] University Of Education.Okara Campus Introduction To C.S & Python
  • 15. University Of Education.Okara Campus Introduction To C.S & Python Month Number Position Jan 1 0 Feb 2 3 Mar 3 6 Apr 4 9  To get the correct position, subtract one from the month number and multiply by three
  • 16. # month.py # A program to print the abbreviation of a month, given its number def main(): # months is used as a lookup table months = "JanFebMarAprMayJunJulAugSepOctNovDec" n = eval(input("Enter a month number (1-12): ")) # compute starting position of month n in months pos = (n-1) * 3 # Grab the appropriate slice from months monthAbbrev = months[pos:pos+3] # print the result print ("The month abbreviation is", monthAbbrev + ".") University Of Education.Okara Campus Introduction To C.S & Python
  • 17. >>> main() Enter a month number (1-12): 1 The month abbreviation is Jan. >>> main() Enter a month number (1-12): 12 The month abbreviation is Dec. One weakness – this method only works where the potential outputs all have the same length. University Of Education.Okara Campus Introduction To C.S & Python
  • 18.  Strings are always sequences of characters, but lists can be sequences of arbitrary values.  Lists can have numbers, strings, or both! myList = [1, "Spam ", 4, "U"] University Of Education.Okara Campus Introduction To C.S & Python
  • 19.  We can use the idea of a list to make our previous month program even simpler!  We change the lookup table for months to a list: months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] University Of Education.Okara Campus Introduction To C.S & Python
  • 20.  To get the months out of the sequence, do this: monthAbbrev = months[n-1] Rather than this: monthAbbrev = months[pos:pos+3] University Of Education.Okara Campus Introduction To C.S & Python
  • 21. # month2.py # A program to print the month name, given it's number. # This version uses a list as a lookup table. def main(): # months is a list used as a lookup table months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] n = eval(input("Enter a month number (1-12): ")) print ("The month abbreviation is", months[n-1] + ".") main()  Since the list is indexed starting from 0, the n-1 calculation is straight-forward enough to put in the print statement without needing a separate step. University Of Education.Okara Campus Introduction To C.S & Python
  • 22.  This version of the program is easy to extend to print out the whole month name rather than an abbreviation! months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] University Of Education.Okara Campus Introduction To C.S & Python
  • 23.   #include<iostream> #include<conio.h> using namespace std; int main() { int ict, isl, eng, math, basic, avg; cout<<"Enter the Marks of 5 subjects="; cin>>ict>>isl>>eng>>math>>basic; avg=(ict+isl+eng+math+basic)/5; if(avg>=80 && avg<=100) cout<<"4 GPA"; else if (avg>=70 && avg<80) cout<<"3 GPA"; else if (avg>=60 && avg <70) cout<<"2 GPA"; else if (avg>=50 && avg < 60) cout<<"1 GPA"; else cout<<"your are fail ";  getche(); } University Of Education.Okara Campus Introduction To C.S & Python
  • 24. 1. Computer Science 9-10 2. WikiPedia 3. www.onlineitlearners.tk 4. www.python.org 5. www.csc.villanova.edu/~map/nlp/pyth on1 6. Udemy Online course University Of Education.Okara Campus