Python Tutorial 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Implement function test() that takes as input one integer and prints ‘Negative’,

‘Zero’, or ‘Positive’ depending on its value.


>>> test(-3) Negative
>>>test(0) Zero
>>> test(3) Positive

Syntax:

integer= input("Enter integer: ")


def test():
if int(integer) < 0:
print("Negative")
elif int(integer) == 0:
print("Zero")
else:
print("Positive")
test()

Output:

(a). (b).

(c).
Write function mult3() that takes as input a list of integers and prints only the
multiples of 3, one per line.

>>> mult3([3, 1, 6, 2, 3, 9, 7, 9, 5, 4, 5)
3
6
3
9
9

Syntax:
lsts=input("Enter list of integers:").split(",")
lst= [int(i) for i in lsts]
def mult3():
for x in lst:
if x % 3 == 0:
print(x)

mult3()

Output:
Implement the function vowels() that takes as input a string and prints the indexes
of all vowels in the string. Hint: a vowel can be defined as any character in string
‘aeiouAEIOU’

>>> vowels(‘Hello WORLD’)


1
4
7

Syntax:
vowls=['a','e','i','o','u','A','E','I','O','U']
words=input("Enter words:")
def vowels():
for i in range(len(words)):
if words[i] in vowls:
print (i)
vowels()

Output:
Implement function indexes() that takes as input a word (as a string ) and a one
character letter (as a string) and returns a list of indexes at which the letter occurs
in the word.
>>> indexes (‘mississippi’, ‘s’)
[2, 3, 5, 6]
>>> indexes (‘mississippi’, ‘i’)
[1, 4, 7, 10]
>>> indexes (‘mississippi’ , ‘a’)

Syntax:
def indexes(word,letter):
list=[]
for index, i in enumerate(word):
if i == letter:
list.append(index)
return list
word= input("enter string:")
letter= input("enter character:")
print(indexes(word, letter))

Output:

(a). (b.)

(c).
Write Function doubles() that takes as input a list of integers and outputs the
integers in the list that are exactly twice the previous integer in the list, one per
line.

>>> doubles([3, 0, 1, 2, 3, 6, 2, 4, 5, 6, 5]) 2 6 4

Syntax:
list =[int(i) for x in input("Enter list of
integers:").split(",")]
def doubles(list):
if len(list) > 1:
for x in range(1, len(list)):
if(list[x] == 2*list[x - 1]):
print(list[i])
return
print(doubles(list))

Output:
Implement function four_letter() that takes as input a list of words (i.e., strings)
and returns the sublist of all four letter words in the list.
>>> four_letter([ ‘dog’, ‘letter’, ‘stop’, ‘door’, ‘bus’, ‘dust’])
[‘stop’, ‘door’, ‘dust’]

Syntax:
words=input("Enter word list:").split(",")
def four_letter(words):
listed =[]
for x in words:
if len(x)==4:
listed.append(x)
return listed

print(four_letter(words))

Output:
Write a function in Both that takes two lists and returns True if there is an item that
is common to both lists and False otherwise.

>>> inBoth ([3, 2, 5, 4, 7], [9, 0, 1, 3])


True

Syntax:
def inBoth(listA, listB):
for list1 in listA:
for list2 in listB:
if list1 == list2:
return True
return False
listA=input("Enter list A:").split(",")
listB=input("Enter list B:").split(",")
print(inBoth(listA, listB))

Output:

You might also like