The document discusses Python strings, functions, and methods. It provides instructions for a lab exercise on evaluating string expressions and accessing substrings using indexing and slicing. It also introduces various Python data types like strings, lists, and numbers. The document compares Python to C and Java by discussing equivalent operations like variable assignment, data types, string concatenation, slicing, and deletion statements. It categorizes Python as having more flexible data types than C and Java.
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]
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?