0% found this document useful (0 votes)
184 views13 pages

Python Exercises for Beginners

This document contains 6 coding exercises to practice defining functions in Python. The exercises involve tasks like finding the intersection of two lists, implementing the FizzBuzz game, finding the maximum or minimum of a list based on whether it is even or odd, checking if a list contains a word, modifying a list by adding to negative numbers and finding the minimum, and returning the square of the 5th element of a list. Solutions to each exercise are not shown.

Uploaded by

Federica Caruso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views13 pages

Python Exercises for Beginners

This document contains 6 coding exercises to practice defining functions in Python. The exercises involve tasks like finding the intersection of two lists, implementing the FizzBuzz game, finding the maximum or minimum of a list based on whether it is even or odd, checking if a list contains a word, modifying a list by adding to negative numbers and finding the minimum, and returning the square of the 5th element of a list. Solutions to each exercise are not shown.

Uploaded by

Federica Caruso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Luiss

Libera Università Internazionale


degli Studi Sociali Guido Carli

Digital Transformation
and Emerging
Technologies
Coding Lab 9
Exercise 1

Define the function list_intersection that takes two lists as input


and returns a new list containing only the elements that are
common to both lists.
Solution
Exercise 2

Implement the FizzBuzz game. Define a function that takes a number as


input and prints the FizzBuzz sequence (from 1 to the input number, but
print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz"
for multiples of both.
Example:
input -> 15
output -> 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14,
FizzBuzz
Solution
Exercise 3

• Define a function that takes a list of integers as input.


• The function calculates the maximum and then:
• prints the maximum if the maximum is even
• prints the minimum if the maximum is odd
Solution
Exercise 4

• Define a function that, given a list of strings, checks that among


the elements present there is the word "Luiss" (excluding quotes).
• If present, the function will return the string "yes", otherwise
"no".
Solution
Exercise 5

• Define a function that, given a list of numbers as input, adds 100


to all the negative elements of the list and then returns the
minimum element.
• Example: if the input is [-10, 33, 30, -90, 20, 55] then the
modified list will be [90,33,30,10,20,55] and will be printed 10,
that is the minimum element.
Solution
Exercise 6

• Define a function that given a list of integers as input returns the


square of the fifth element of the input list. If the element does
not exist, the function returns 0.
• For example: if as input I have the list [3,2,1,4,6,5,7] the function
will return 36, i.e., the fifth element (6) raised at the power of 2.
Solution

You might also like