0% found this document useful (0 votes)
51 views17 pages

Python Exercise 3

The document provides examples of coding exercises and their solutions in Python. It includes exercises to define functions to check if all numbers in a list are different, multiply each element in a list by a number, generate lists of odd squares and factorials using while loops, generate the Fibonacci sequence using a for loop, separate lists into positive and negative numbers, find palindrome words in a list, and find numbers between 100 and 200 that are divisible by 7 but not 5. The solutions provide the defined functions to solve each problem.

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)
51 views17 pages

Python Exercise 3

The document provides examples of coding exercises and their solutions in Python. It includes exercises to define functions to check if all numbers in a list are different, multiply each element in a list by a number, generate lists of odd squares and factorials using while loops, generate the Fibonacci sequence using a for loop, separate lists into positive and negative numbers, find palindrome words in a list, and find numbers between 100 and 200 that are divisible by 7 but not 5. The solutions provide the defined functions to solve each problem.

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

Luiss

Libera Università Internazionale


degli Studi Sociali Guido Carli

Digital Transformation
and Emerging
Technologies
Coding Lab 8
Exercise 17

• Define the check_difference function that takes a list of numbers


as parameters and returns True or False according to whether all
the numbers are different from each other.
Solution
In this function, unique_numbers is a list
used to store unique numbers encountered
while iterating through the input list numbers.
For each number in numbers, the function
checks if it's already in unique_numbers. If it
is, the function immediately returns False
because a duplicate has been found. If the
number is not in unique_numbers, it is added
to the list. After checking all numbers, if no
duplicates are found, the function returns True.
This approach uses a for loop and a list to
efficiently check for duplicates and determine
if all numbers in the input list are different
from each other.
Exercise 18

• Define the multiply_list function that takes as parameters a list of


numbers and a number n and returns a new list with each element
multiplied by n.
Solution
Exercise 18 a

• Write a function generate_odd_squares(n) that takes an integer


n as input and returns a list containing the squares of all odd
numbers from 1 to n using a while loop.
Solution
Exercise 18 b

• Write a function calculate_factorial(n) that takes an integer n as


input and returns the factorial of n using a while loop.
• Ex. FACTORIAL (4) = 4x3x2x1
Solution
Exercise 18 c

• Write a function generate_fibonacci_sequence(n) that takes an integer


n as input and returns a list containing the first n numbers in the
Fibonacci sequence with for cycle
HINT: The Fibonacci Sequence is a series of numbers. The next number
is found by adding up the two numbers before it. The first two
numbers are 0 and 1.
For example, 0, 1, 1, 2, 3, 5, 8, 13, 21. The next number in this series
above is 13+21 = 34.
Solution
Exercise 19

• Write the sep_pos_neg function that takes a list of positive and


negative numbers as parameters and returns two new list, one
containing positive numbers and one the negative numbers
Solution
Exercise 20

• Define the palindrome_list function that given a list of words as


parameter returns the list of only words that are palindrome.
• HINTS: use [::-1] for obtaining the reverse of the words
Solution
Exercise 21

• Define a function that finds all such numbers which are divisible
by 7 but are not a multiple of 5, between 100 and 200 (both
included) and returns a list containing them.
Solution

You might also like