SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Semelhante a 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
 

Mais de university of education,Lahore

Mais de 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
 

Último

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 

Último (20)

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

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