Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
63 views

Python Tutorial 4

The remaining examples demonstrate printing formatted labels, implementing

Uploaded by

queen setilo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Python Tutorial 4

The remaining examples demonstrate printing formatted labels, implementing

Uploaded by

queen setilo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Start by executing the assignments:

S= ‘01234566789’

Now write expressions using string s and the indexing operator that evaluate to:

(a) ‘234’
(b) ‘78’
(c) ‘1234567’
(d) ‘0123’
(e) ‘789’

Syntax:
s = "0123456789"
a = s[2:5]
print("(a)"+ a)

b = s[7:9]
print("(b)"+ b)

c = s[1:8]
print("(c)"+ c)

d = s[0:4]
print("(d)"+ d)

e = s[7:10]
print("(e)"+ e)
Output:
Assuming that variable forecast has been assigned string
‘It will be a sunny day today’

Write Python statements corresponding to these assignments:

(a) To variable count, the number of occurrences of string ‘day’ in string forecast.
(b) To variable weather, the index where substring ‘sunny’ starts.
(c) To variable change, a copy of forecast in which every occurrence of substring
‘sunny’ is replaced by ‘cloudy’.

Syntax:
forecast=("It will be a sunny day today")
count=forecast.count("sunny")
print(count)

index=forecast.index("sunny")
print(index)

replace=forecast.replace("sunny","cloudy")
print(replace)

Output:
Write a statement that prints the values of variables last, first, and middle in one
line, separated by a horizontal tab character. (The Python escape sequence for the
horizontal tab character is \t.) If the variables are assigned like this:
>>> last = ‘Smith’
>>> first = ‘John’
>>> middle = ‘Paul’

The output should be:

Smith John Paul

Syntax:
last = "Smith"
first = "John"
middle = "Paul"

print(last,first,"\t",middle)

Output:
Write function even() that takes a positive integer n as input and prints on the
screen all numbers between, and including, 2 and n divisible by 2 or by 3, using
this output format:

Expected Output:

even(17) 2, ,3, 4, 6, 8, 9, 10, 12, 14, 15, 16

Syntax:
def even():
for x in range(2,int(n)):
if x % 2 == 0 or x%3 ==0:
print (x, end=" ")
n=input("Enter positive integer n:")
even()

Output:
Assume variables first, last, street, number, city, state, zipcode have already been
assigned. Write a print statement that creates a mailing label:

Expected Output:

John Doe

123 Main Street AnyCity, AS 09876 assuming that:

First = ‘John’

Last = ‘Doe’

Street = ‘Main Street’

Number = 123

City = ‘AnyCity’

State = ‘AS’

Zipcode = ‘09876’

Syntax:
first = "John"
last = "Doe"
street = "main Street"
number = 123
city="AnyCity"
state="AS"
zipcode="09786"

print(first,last)
print(number,street,city + ",",state,zipcode)

Output:
Implement function roster() that takes a list containing student information and
print out a roster, as shown below. The student in formation, consisting of the
student’s last name, first name, class, and average course grade, will be stored in
that order in a list. Therefore, the input list is a list of lists. Make sure the roster
printed out has 10 slots for every string value and 8 for the grade, including 2 slots
for the decimal part.

Expected Output:

students = []
students.append(["Last","First" ,"class","Average Grade"])
students.append(["DeMoines", "Jim", "Sophomore", 3.45])
students.append(["Pierre", "Sophie", "Sophomore", 4.0])
students.append(["Columbus", "Maria", "Senior", 2.5])
students.append(["Phoenix", "River", "Junior", 2.45])
students.append(["Olympis", "Edgar", "Junior", 3.99])

roster(students)

Syntax:
def roster():
students = []
students.append(["Last","First" ,"class","Average Grade"])
students.append(["DeMoines", "Jim", "Sophomore", 3.45])
students.append(["Pierre", "Sophie", "Sophomore", 4.0])
students.append(["Columbus", "Maria", "Senior", 2.5])
students.append(["Phoenix", "River", "Junior", 2.45])
students.append(["Olympis", "Edgar", "Junior", 3.99])

length = [len(str(listvar)) for row in students for listvar


in row]
column = max(length)
for row in students:
row = "".join(str(listvar).ljust(column + 2) for listvar
in row)
print(row)
roster()
Output:
Write function stringCount() that takes two string inputs—a file name and a target
string--- and returns the number of occurrences of the target string in the file.

>>> stringCount(‘example.txt’, ‘line’)

Syntax:
def stringCount():
filename=open(input("Enter file name:"),"r")
x=filename.read().split()
line=print(x.count(input("target string:")))
return line

stringCount()

Output:

You might also like