SlideShare une entreprise Scribd logo
1  sur  13
CS 360 LAB 3: STRINGS, FUNCTIONS, AND METHODS
Objective: The purpose of this lab is to get you comfortable
with using the Python functions and modules.
Note: Python has a lot of modules that come with the language,
which collectively are called the Python Standard Library. The
Python documentation contains the specifications for all the
functions in the library. You can get to it from the link on the
course website, or by going here:
http://docs.python.org/library/
1) Strings
Strings have many handy methods, whose specifications can be
found at the following URL:
http://docs.python.org/2/library/stdtypes.html#string-methods
Strings are a built-in type, so although there is a module named
string, you do not need it for basic string operations.
To reference substrings (or even individual characters) we can
use the bracket notation shown in class. Remember Python
counts characters starting from zero|for example, s[1] is the
second character in s.
One of the things to remember about strings is that they are
immutable. Methods or sequence operations return new strings;
they never modify the original string object.
Evaluating string Expressions. Before starting with the table
below, enter the following assignment statement into the Python
shell:
s = "Hello World!"
Once you have done that, you will use the string stored in s to
_ll out the table below.
Expression Expected
Expected Value
Calculated value
Reason for calculated value
s[2]
s[15]
s[1:5]
s[:5]
s[5:]
"e" in s
"x" in s
s.index("e")
s.index("x")
s.index("l", 5)
where to start looking)
s.find("e")
s.find("x")
s.islower()
s[1:5].islower()
Prepared By: Kajal Nusratullah Page 1
Programming Languages CS 360
Lab Tutorial # 1
Week no 1 and 2
Visit the following link and Install Python 3.
www.python.org
Beginning with python
Type the following text to the right of the Python prompt and
press the Enter key:
>>> print ("Hello, Python!")
Assigning Values to Variables:
Python variables do not have to be explicitly declared to reserve
memory space. The declaration happens
automatically when you assign a value to a variable. The equal
sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the
variable and the operand to the right of the = operator is
the value stored in the variable. For example:
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print counter
print miles
print name
Here, 100, 1000.0 and "John" are the values assigned to counter,
miles and name variables, respectively. While
running this program, this will produce the following result:
100
1000.0
John
Write down a program in C and Java that will
declare the same variables, assign the same
values and discuss the difference
Prepared By: Kajal Nusratullah Page 2
Multiple Assignment:
Python allows you to assign a single value to several variables
simultaneously. For example:
a = b = c = 1
Here, an integer object is created with the value 1, and all three
variables are assigned to the same memory location.
You can also assign multiple objects to multiple variables. For
example:
a, b, c = 1, 2, "john"
Here, two integer objects with values 1 and 2 are assigned to
variables a and b, and one string object with the value
"john" is assigned to the variable c.
Standard Data Types:
The data stored in memory can be of many types. For example,
a person's age is stored as a numeric value and his
or her address is stored as alphanumeric characters. Python has
various standard types that are used to define the
operations possible on them and the storage method for each of
them.
Python has five standard data types:
Write down a program in C and
java that will show the multiple
assignment statements
List down the standard data type of
C and Java
Prepared By: Kajal Nusratullah Page 3
Python Numbers:
Number data types store numeric values. They are immutable
data types which mean that changing the value of a
number data type results in a newly allocated object.
Number objects are created when you assign a value to them.
For example:
var1 = 1
var2 = 10
You can also delete the reference to a number object by using
the del statement. The syntax of the del statement is:
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the
del statement. For example:
del var
del var_a, var_b
Python Strings:
Strings in Python are identified as a contiguous set of characters
in between quotation marks. Python allows for either
pairs of single or double quotes. Subsets of strings can be taken
using the slice operator ( [ ] and [ : ] ) with indexes
starting at 0 in the beginning of the string and working their
way from -1 at the end.
The plus ( + ) sign is the string concatenation operator and the
asterisk ( * ) is the repetition operator. For example:
#!/usr/bin/python
str = 'Hello World!'
print (str) # Prints complete string
print (str[0]) # Prints first character of the string
print (str[2:5]) # Prints characters starting from 3rd to 5th
print (str[2:]) # Prints string starting from 3rd character
print (str * 2) # Prints string two times
print (str + "TEST") # Prints concatenated string
This will produce the following result:
Hello World!
H
llo
llo World!
Do we have any statement like del in java , A statement that
can
delete a single object or multiple objects. If yes illustrate how it
works?
Prepared By: Kajal Nusratullah Page 4
Hello World!Hello World!
Hello World!TEST
Python Lists:
Lists are the most versatile of Python's compound data types. A
list contains items separated by commas and
enclosed within square brackets ([]). To some extent, lists are
similar to arrays in C. One difference between them is
that all the items belonging to a list can be of different data
type.
The values stored in a list can be accessed using the slice
operator ( [ ] and [ : ] ) with indexes starting at 0 in the
beginning of the list and working their way to end -1. The plus (
+ ) sign is the list concatenation operator, and the
asterisk ( * ) is the repetition operator. For example:
#!/usr/bin/python
list = ['abcd',786,2.23,'john',70.2]
tinylist = [123,'john']
print (list) # Prints complete list
print (list[0]) # Prints first element of the list
print (list[1:3]) # Prints elements starting from 2nd till 3rd
print (list[2:]) # Prints elements starting from 3rd element
print (tinylist * 2) # Prints list two times
print (list + tinylist) # Prints concatenated lists
This will produce the following result:
['abcd', 786, 2.23, 'john', 70.200000000000003]
abcd
[786, 2.23]
[2.23, 'john', 70.200000000000003]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']
What operator we use to concat in C and Java?
Are there any built-in function in these languages that will print
the slice
of string.
Prepared By: Kajal Nusratullah Page 5
Exercise:
amount of
money, then reports how much more money he/she requires to
have a
new car that is about 1,50000.
After the above
discussion how do
you categories
python as
comparison of C
and Java?
Is python an
ORTHOGONAL
LANGUAGE?
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx

Contenu connexe

Similaire à CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx

C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 

Similaire à CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx (20)

unit 1.docx
unit 1.docxunit 1.docx
unit 1.docx
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
 
Python PPT2
Python PPT2Python PPT2
Python PPT2
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Python
PythonPython
Python
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
 
Python standard data types
Python standard data typesPython standard data types
Python standard data types
 
Chapter - 2.pptx
Chapter - 2.pptxChapter - 2.pptx
Chapter - 2.pptx
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
CS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdf
 
Python 3.x quick syntax guide
Python 3.x quick syntax guidePython 3.x quick syntax guide
Python 3.x quick syntax guide
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
 
AI_2nd Lab.pptx
AI_2nd Lab.pptxAI_2nd Lab.pptx
AI_2nd Lab.pptx
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Shanks
ShanksShanks
Shanks
 
Introduction To Programming with Python-3
Introduction To Programming with Python-3Introduction To Programming with Python-3
Introduction To Programming with Python-3
 
Python quick guide
Python quick guidePython quick guide
Python quick guide
 

Plus de faithxdunce63732

Assignment DetailsScenario You are an investigator for Child .docx
Assignment DetailsScenario You are an investigator for Child .docxAssignment DetailsScenario You are an investigator for Child .docx
Assignment DetailsScenario You are an investigator for Child .docx
faithxdunce63732
 
Assignment DetailsScenario Generally, we have considered sexual.docx
Assignment DetailsScenario Generally, we have considered sexual.docxAssignment DetailsScenario Generally, we have considered sexual.docx
Assignment DetailsScenario Generally, we have considered sexual.docx
faithxdunce63732
 
Assignment DetailsPower’s on, Power’s Off!How convenient is.docx
Assignment DetailsPower’s on, Power’s Off!How convenient is.docxAssignment DetailsPower’s on, Power’s Off!How convenient is.docx
Assignment DetailsPower’s on, Power’s Off!How convenient is.docx
faithxdunce63732
 
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docxAssignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
faithxdunce63732
 
Assignment DetailsMN551Develop cooperative relationships with.docx
Assignment DetailsMN551Develop cooperative relationships with.docxAssignment DetailsMN551Develop cooperative relationships with.docx
Assignment DetailsMN551Develop cooperative relationships with.docx
faithxdunce63732
 
Assignment DetailsInfluence ProcessesYou have been encourag.docx
Assignment DetailsInfluence ProcessesYou have been encourag.docxAssignment DetailsInfluence ProcessesYou have been encourag.docx
Assignment DetailsInfluence ProcessesYou have been encourag.docx
faithxdunce63732
 

Plus de faithxdunce63732 (20)

Assignment DetailsScenario You are member of a prisoner revie.docx
Assignment DetailsScenario You are member of a prisoner revie.docxAssignment DetailsScenario You are member of a prisoner revie.docx
Assignment DetailsScenario You are member of a prisoner revie.docx
 
Assignment DetailsScenario You are an investigator for Child .docx
Assignment DetailsScenario You are an investigator for Child .docxAssignment DetailsScenario You are an investigator for Child .docx
Assignment DetailsScenario You are an investigator for Child .docx
 
Assignment DetailsScenario You are a new patrol officer in a .docx
Assignment DetailsScenario You are a new patrol officer in a .docxAssignment DetailsScenario You are a new patrol officer in a .docx
Assignment DetailsScenario You are a new patrol officer in a .docx
 
Assignment DetailsScenario Generally, we have considered sexual.docx
Assignment DetailsScenario Generally, we have considered sexual.docxAssignment DetailsScenario Generally, we have considered sexual.docx
Assignment DetailsScenario Generally, we have considered sexual.docx
 
Assignment DetailsPower’s on, Power’s Off!How convenient is.docx
Assignment DetailsPower’s on, Power’s Off!How convenient is.docxAssignment DetailsPower’s on, Power’s Off!How convenient is.docx
Assignment DetailsPower’s on, Power’s Off!How convenient is.docx
 
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docxAssignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
 
Assignment DetailsPart IRespond to the following.docx
Assignment DetailsPart IRespond to the following.docxAssignment DetailsPart IRespond to the following.docx
Assignment DetailsPart IRespond to the following.docx
 
Assignment DetailsPlease discuss the following in your main post.docx
Assignment DetailsPlease discuss the following in your main post.docxAssignment DetailsPlease discuss the following in your main post.docx
Assignment DetailsPlease discuss the following in your main post.docx
 
Assignment DetailsPennsylvania was the leader in sentencing and .docx
Assignment DetailsPennsylvania was the leader in sentencing and .docxAssignment DetailsPennsylvania was the leader in sentencing and .docx
Assignment DetailsPennsylvania was the leader in sentencing and .docx
 
Assignment DetailsPart IRespond to the followingReview .docx
Assignment DetailsPart IRespond to the followingReview .docxAssignment DetailsPart IRespond to the followingReview .docx
Assignment DetailsPart IRespond to the followingReview .docx
 
Assignment DetailsPart IRespond to the following questio.docx
Assignment DetailsPart IRespond to the following questio.docxAssignment DetailsPart IRespond to the following questio.docx
Assignment DetailsPart IRespond to the following questio.docx
 
Assignment DetailsPart IRespond to the following questions.docx
Assignment DetailsPart IRespond to the following questions.docxAssignment DetailsPart IRespond to the following questions.docx
Assignment DetailsPart IRespond to the following questions.docx
 
Assignment DetailsOne thing that unites all humans—despite cultu.docx
Assignment DetailsOne thing that unites all humans—despite cultu.docxAssignment DetailsOne thing that unites all humans—despite cultu.docx
Assignment DetailsOne thing that unites all humans—despite cultu.docx
 
Assignment DetailsMN551Develop cooperative relationships with.docx
Assignment DetailsMN551Develop cooperative relationships with.docxAssignment DetailsMN551Develop cooperative relationships with.docx
Assignment DetailsMN551Develop cooperative relationships with.docx
 
Assignment DetailsInfluence ProcessesYou have been encourag.docx
Assignment DetailsInfluence ProcessesYou have been encourag.docxAssignment DetailsInfluence ProcessesYou have been encourag.docx
Assignment DetailsInfluence ProcessesYou have been encourag.docx
 
Assignment DetailsIn this assignment, you will identify and .docx
Assignment DetailsIn this assignment, you will identify and .docxAssignment DetailsIn this assignment, you will identify and .docx
Assignment DetailsIn this assignment, you will identify and .docx
 
Assignment DetailsFinancial statements are the primary means of .docx
Assignment DetailsFinancial statements are the primary means of .docxAssignment DetailsFinancial statements are the primary means of .docx
Assignment DetailsFinancial statements are the primary means of .docx
 
Assignment DetailsIn this assignment, you will identify a pr.docx
Assignment DetailsIn this assignment, you will identify a pr.docxAssignment DetailsIn this assignment, you will identify a pr.docx
Assignment DetailsIn this assignment, you will identify a pr.docx
 
Assignment DetailsHealth information technology (health IT) .docx
Assignment DetailsHealth information technology (health IT) .docxAssignment DetailsHealth information technology (health IT) .docx
Assignment DetailsHealth information technology (health IT) .docx
 
Assignment DetailsDiscuss the followingWhat were some of .docx
Assignment DetailsDiscuss the followingWhat were some of .docxAssignment DetailsDiscuss the followingWhat were some of .docx
Assignment DetailsDiscuss the followingWhat were some of .docx
 

Dernier

Dernier (20)

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...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
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
 

CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx

  • 1. CS 360 LAB 3: STRINGS, FUNCTIONS, AND METHODS Objective: The purpose of this lab is to get you comfortable with using the Python functions and modules. Note: Python has a lot of modules that come with the language, which collectively are called the Python Standard Library. The Python documentation contains the specifications for all the functions in the library. You can get to it from the link on the course website, or by going here: http://docs.python.org/library/ 1) Strings Strings have many handy methods, whose specifications can be found at the following URL: http://docs.python.org/2/library/stdtypes.html#string-methods Strings are a built-in type, so although there is a module named string, you do not need it for basic string operations. To reference substrings (or even individual characters) we can use the bracket notation shown in class. Remember Python counts characters starting from zero|for example, s[1] is the second character in s. One of the things to remember about strings is that they are immutable. Methods or sequence operations return new strings; they never modify the original string object. Evaluating string Expressions. Before starting with the table below, enter the following assignment statement into the Python shell: s = "Hello World!" Once you have done that, you will use the string stored in s to _ll out the table below. Expression Expected Expected Value Calculated value Reason for calculated value s[2]
  • 2. s[15] s[1:5] s[:5] s[5:] "e" in s "x" in s s.index("e") s.index("x") s.index("l", 5)
  • 3. where to start looking) s.find("e") s.find("x") s.islower() s[1:5].islower() Prepared By: Kajal Nusratullah Page 1 Programming Languages CS 360 Lab Tutorial # 1 Week no 1 and 2 Visit the following link and Install Python 3.
  • 4. www.python.org Beginning with python Type the following text to the right of the Python prompt and press the Enter key: >>> print ("Hello, Python!") Assigning Values to Variables: Python variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. For example: counter = 100 # An integer assignment miles = 1000.0 # A floating point name = "John" # A string print counter print miles print name Here, 100, 1000.0 and "John" are the values assigned to counter,
  • 5. miles and name variables, respectively. While running this program, this will produce the following result: 100 1000.0 John Write down a program in C and Java that will declare the same variables, assign the same values and discuss the difference Prepared By: Kajal Nusratullah Page 2 Multiple Assignment: Python allows you to assign a single value to several variables simultaneously. For example: a = b = c = 1 Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example:
  • 6. a, b, c = 1, 2, "john" Here, two integer objects with values 1 and 2 are assigned to variables a and b, and one string object with the value "john" is assigned to the variable c. Standard Data Types: The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard types that are used to define the operations possible on them and the storage method for each of them. Python has five standard data types: Write down a program in C and
  • 7. java that will show the multiple assignment statements List down the standard data type of C and Java Prepared By: Kajal Nusratullah Page 3 Python Numbers: Number data types store numeric values. They are immutable data types which mean that changing the value of a number data type results in a newly allocated object. Number objects are created when you assign a value to them. For example: var1 = 1 var2 = 10 You can also delete the reference to a number object by using the del statement. The syntax of the del statement is: del var1[,var2[,var3[....,varN]]]] You can delete a single object or multiple objects by using the del statement. For example:
  • 8. del var del var_a, var_b Python Strings: Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end. The plus ( + ) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator. For example: #!/usr/bin/python str = 'Hello World!' print (str) # Prints complete string print (str[0]) # Prints first character of the string print (str[2:5]) # Prints characters starting from 3rd to 5th print (str[2:]) # Prints string starting from 3rd character print (str * 2) # Prints string two times
  • 9. print (str + "TEST") # Prints concatenated string This will produce the following result: Hello World! H llo llo World! Do we have any statement like del in java , A statement that can delete a single object or multiple objects. If yes illustrate how it works? Prepared By: Kajal Nusratullah Page 4 Hello World!Hello World! Hello World!TEST
  • 10. Python Lists: Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type. The values stored in a list can be accessed using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator. For example: #!/usr/bin/python list = ['abcd',786,2.23,'john',70.2] tinylist = [123,'john'] print (list) # Prints complete list print (list[0]) # Prints first element of the list print (list[1:3]) # Prints elements starting from 2nd till 3rd print (list[2:]) # Prints elements starting from 3rd element print (tinylist * 2) # Prints list two times print (list + tinylist) # Prints concatenated lists This will produce the following result:
  • 11. ['abcd', 786, 2.23, 'john', 70.200000000000003] abcd [786, 2.23] [2.23, 'john', 70.200000000000003] [123, 'john', 123, 'john'] ['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john'] What operator we use to concat in C and Java? Are there any built-in function in these languages that will print the slice of string. Prepared By: Kajal Nusratullah Page 5
  • 12. Exercise: amount of money, then reports how much more money he/she requires to have a new car that is about 1,50000. After the above discussion how do you categories python as comparison of C and Java? Is python an ORTHOGONAL LANGUAGE?