Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions students/tsegas/session03/list_lab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# list lab execises from session-03

import random
import string
import sys
import os

# List of fruits
mylist = ["apples", "pears","oranges","peaches"]

# Display the list
print('\n',"List of fruit",mylist,'\n')

# Prompt for another type of fruit
newfruit = input('Enter a type of fruit:')

mylist.append(newfruit)

mylen = len(mylist)

# Display the list with the new fruit added
print("List of fruit with the {} added".format(newfruit),mylist,'\n')

# Prompt for a number
num = int(input('Enter a number from 1 to {:d}:'.format(mylen)))
#int(input("Please enter an integer: "))

for i in range(0,mylen):
if (i == num-1):
print("The fruit at that number is",mylist[i],'\n')

addfruit = ["lemon"]
mylist = addfruit + mylist

# Display the list with lemon added
print("List of fruit with lemon added",mylist)

mylist.insert(0,"kiwi")

# Display the list with kiwi added
print("List of fruit with kiwi added",mylist,'\n')

for i in mylist:
# find fruits that start with a 'P'
if (i[0] == "p"):
print("This fruit starts with p",i,'\n')


############################## Series 2 ---- actions #######################################
# Display the list
print("List of fruit:",mylist,'\n')

# Remove the last fruit
mylist.pop()

# Display the list
print("List of fruit with the last item removed:",mylist,'\n')

# Double the elements in the list
mylist = mylist + mylist
# Display the list
print("List of fruit.... multiplied by 2:",mylist,'\n')

# Prompt for a fruit to delete
delfruit = input('Enter a fruit to be deleted:')

temp = mylist.copy()
lenth = len(temp)

# for loop to go through the list and delete occurances of the seleted fruit
for i in range(0,(lenth-1)):
if (temp[i] == delfruit):
#delete the spcified fruit
mylist.remove(temp[i])
else:
print("This fruit was not selected for removal",temp[i],'\n')

# Display the list with the selected fruit removed
print("List of fruit with all occurances of {} removed".format(delfruit),mylist,'\n')

############################## Series 3 ---- actions #######################################

# Change each fruit's first letter to capital
ntemp = mylist.copy()
for i, elem in enumerate(ntemp):
s = elem
mylist.remove(elem)
# Set the first letter in each item in the list to upper case
s = s.capitalize()
elem = s
# insert the the capitalized item right back in its place
mylist.insert(i,elem)

# Display the list with each item capitalized
print("List of fruit with each item capitalized ...:",mylist,'\n')

# Change each fruit's first letter to lower case
ntemp = mylist.copy()
for i, elem in enumerate(ntemp):
s = elem
mylist.remove(elem)
# Set the first letter in each item in the list to lower case
s = s.lower()
elem = s
# insert the the capitalized item right back in its place
mylist.insert(i,elem)

# Display the list with each item in lower case
print("List of fruit with each item in lower case ...:",mylist,'\n')

tlist = mylist.copy()
# Loop through the list and ask the user if they like the fruit item
for i in tlist:
# Prompt for a fruit to delete
answ = (input('Do you like {}:'.format(i)))
if (answ == "no"):
# Remove the item
mylist.remove(i)
elif (answ == "yes"):
print('you like this fruit:',i,'\n')
# insist on a "yes" or "no" answer
else:
answ = input('Enter "yes" or "no" Please:')
if (answ == "no"):
# Remove the item
mylist.remove(i)
elif (answ == "yes"):
print('you like this fruit:',i,'\n')

# Display the list of liked fruits
print("List of fruit with the ones you don't like removed:",mylist,'\n')

############################## Series 4 ---- actions #######################################

# copy the last list
nextlist = mylist.copy()
print("List of fruit displayed... ",nextlist,'\n')
nlist = []
ilist = []
# loop through the list of fruit items
for i in nextlist:
n = 1
strg = ''
# Loop through each fruit item to get each letter that needs to be reversed
for j in i:
ln = len(i)
last = (i[ln-n])
n = n+1
if (n > 1):
strg = ''.join((strg,last))
ilist = [strg]
nlist = nlist + ilist

# Display the list of liked fruits
print("List of fruit with letters in each fruit reversed...",nlist,'\n')

# copy the list
lastlist = nextlist.copy()

# Remove last item in the list
lastlist.pop()

# Display the original list
print("The original list of fruit displayed... ",nextlist,'\n')


# Display the original list
print("The original list of fruit with the last item deleted... ",lastlist,'\n')
31 changes: 31 additions & 0 deletions students/tsegas/session03/slice_lab1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#return sequence with last and first items changed

#original seuqence
seq1 = [4,5,6,7,8,9]

# assign the first element to temp
temp = seq1[0]

#length of the sequence
length = len(seq1)

#assign the lat element to temp1
temp1 = seq1[length-1]

#print the original sequence
print("original sequence",seq1)

#remove the first element
seq1.pop(0)

#remove the last element
seq1.pop(length-2)
print("sequence with first and last element removed",seq1)

#append the first element which was stored in temp
seq1.append(temp)

#insert the last element which was stored in temp1
seq1.insert(0,temp1)
print("sequence with the first and last items exchanged",seq1)

23 changes: 23 additions & 0 deletions students/tsegas/session03/slice_lab2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#return sequence with every other items removed

#original seuqence
seq1 = [4,5,6,7,8,9]

#length of the sequence
length = len(seq1)

#print the original sequence
print("original sequence",seq1)

n = 1
lim = abs(length/2)
# if sequence length is half the number of original items stop looping
while (length > lim):

seq1.pop(n)
n=n+1
if (n > lim):
print("sequence with every other item removed...",seq1)



29 changes: 29 additions & 0 deletions students/tsegas/session03/slice_lab3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#return a sequence with the first and last 4 items removed,
#and every other item in between

#original seuqence
seq1 = [1,2,3,4,5,6,7,8,9,10,'a','b','c','d']

#length of the sequence
length = len(seq1)

#print the original sequence
print("original sequence",seq1)

# remove the first 4 items and the last four items from the sequence
seq2 = seq1[4:(length-4)]
print("sequence with first and last 4 items removed",seq2)

n = 1
length2 = len(seq2)
lim = abs(length2/2)
# if sequence length is half the number of original items stop looping
while (length2 > lim):

seq2.pop(n)
n=n+1
if (n > lim):
print("sequence with every other item in between...",seq2)



12 changes: 12 additions & 0 deletions students/tsegas/session03/slice_lab4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#return a sequence reversed (just with slicing)

#original seuqence
seq1 = [1,2,3,4,5,6,7,8,9,10,'a','b','c','d']

#print the original sequence
print("original sequence",seq1)

seq2 = seq1[::-1]
print("sequence reversed is:.......",seq2)


27 changes: 27 additions & 0 deletions students/tsegas/session03/slice_lab5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#return a sequence with the middle third, then last third, then the first third in the new order

#original seuqence
seq1 = [1,2,3,4,5,6,7,8,9,10,'a','b']

length = len(seq1)

#print the original sequence
print("original sequence",seq1)

#a third of the length of the list is
third = int(abs(length/3))

# split the sequence into three equal parts
seq2 = seq1[0:(third)]
seq3 = seq1[third:(third*2)]
seq4 = seq1[(third*2):(third*3)]

print("first ..........3rd",seq2)
print("2nd ..........3rd",seq3)
print("3rd ..........3rd",seq4)

# combine the sequence in a different order
nseq = seq3 + seq4 +seq2
print("new sequence:..........",nseq)


42 changes: 42 additions & 0 deletions students/tsegas/session03/str_format_lab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# String format lab execises from session-03
# produce 'file_002 : 123.46, 1e+04'

import random
import string
import sys
import os
import math

#passnum function to pass the 3 numbers
def passnum(x,y,z):
print("The first 3 numbers are: {:d}, {:d}, {:d}".format(x,y,z))
n = ('{:d}{:d}{:d}'.format(x,y,z))
return n

#call the function
n=passnum(x=7,y=8,z=9)

# Convert the returned value to an integer
n = int(n)
f1 = '{:.4f}'.format(.4567).lstrip('0')

# Convert to float
f = float(f1)

m = ('{:d}{:.4f}'.format(n,f).lstrip('0'))

# Convert to float
m = float(m)
# This was the only way for now I could get rid of the leading zero
m = (m/10)+0.41103

strg = (2,m,10000)
print("Change this............",strg)

a = ('{:d}'.format(strg[0]))
b = ('{:05.2f}'.format(strg[1]))
# Format the last value as an exponent
c = ('{:01.0e}'.format(strg[2]))

print("To this................ file_00{} : {},".format(a,b),c,'\n')

Loading