0% found this document useful (0 votes)
2 views

Tema 1_Introduction to Python Problems

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

Tema 1_Introduction to Python Problems

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

Introduction to Python – Worksheet

LEARNING OUTCOMES
• Employ conditional structures in Python to branch the behavior of a
software
• Employ looping structures in Python to repeat sequences or blocks
of actions
• Define functions in Python to encapsulate and organize code
• Describe the behavior of different types of data structures
• Employ appropriate data structures to create effective software
ARITHMETIC OPERATORS

1. Write a Python script that allows users to convert a given amount of money from
Euros to US Dollars. Both the amount of money and the exchange rate between Euros
and dollars should be input by the user using the keyboard.
2. Write a Python script to calculate the speed of a frictionless fluid at a given point, and
considering the information of the fluid at another point. More specifically, it will make
use of Bernouilli’s equation:
1 1
𝑃1 + 𝜌𝑣12 + 𝜌𝑔ℎ1 = 𝑃2 + 𝜌𝑣22 + 𝜌𝑔ℎ2
2 2
The user should input the pressure at both points, the fluid’s density, both heights,
and the speed at the initial point.
3. Write a Python script that allows users to solve second order equations (𝑎𝑥 2 + 𝑏𝑥 +
𝑐 = 0) by inputting values for coefficients of the second order equation. The script
should inform the user of available solutions, if any.

CONDITIONAL STRUCTURES

4. Write a Python script to determine if a given number is odd or even. The user should
input the number from keyboard.
5. Write a Python script that takes a year as input and determines whether it is a leap
year. A leap year is divisible by 100, but not by 100, unless it is divisible by 400.
6. Write a Python script to determine the amount of damage inflicted to an enemy in a
dungeon crawl board game. In the game, there are 3 classes: warrior, archer, and
mage. For each class, the damage calculation goes as follows:
• Warrior:
i. It depends on his/her strength and dexterity with the following
𝐷𝐸𝑋
formula: 𝐷𝑀𝐺 = 2 ∗ 𝑆𝑇𝑅 +
2
ii. If the warrior wields a two-handed weapon, the amount of damage is
modified by 1.5 factor.
• Archer:
i. The damage depends solely on the dexterity.
ii. If the archer equips elemental arrows, the damage is increased by 50.
iii. The final damage of the archer is reduced by distance, at a rate of 2
damage points per 10 meters.
• Mage:
i. The damage of the mage depends on intelligence and dexterity with
the following formula: 𝐷𝑀𝐺 = 𝐼𝑁𝑇 2 + 0.5 √𝐷𝐸𝑋
7. Write a Python script that allows users to calculate their mobile phone expenses if we
consider the following:
- For the first 120 minutes, the user pays at a rate of 0.03 € per minute.
- Then, up to 250 minutes the user pays at a rate of 0.05 € per minute.
- Finally, from that point onward, the user pays at a rate of 0.1 € per minute.
The user should input the monthly minutes consumed and the script should inform
about the amount to pay in the next bill.

LOOPING STRUCTURES

8. Write a Python script to determine if a given integer number (input from keyboard) is
prime.
9. Write a Python script to allow users to introduce temperature observations until
he/she introduces a special character. Then, the script should calculate the average of
the introduced observations.
10. Write a Python script to allow users to play a number-guessing name. The script should
randomly generate an integer number between 0 and 100. Then, the script should ask
numbers to the user until he/she guesses the randomly generated number. The
software should keep track of the number of attempts.
11. Write a Python script to read a string from keyboard and count the number of vowels
in the string.
12. Write a Python script that reads an integer n from keyboard. Then, the script will
generate and print n random lists of 10 elements with numbers from 0 to 1.
13. Write a Python script that generates two random lists of numbers with the same size
(10 elements) and creates a new list by adding both lists element by element.
14. Write a Python script that reads integer numbers from the keyboard until the user
introduces an odd number. Then, it should return the sum of all added elements.

FUNCTIONS

15. Write a function to determine whether a number is prime.


16. Write a program that determines the first 100 prime numbers. Based your code on the
function written in the previous question.
17. Write a function that takes a numerical list as a parameter and calculates the median
of the elements.
18. Write a function that takes a numerical list as a parameter and calculates the average
of the elements.
19. Write a function called greater_than_average that takes a number x as parameter and
a numerical Python list. The function should return True if x is greater than the average
of the array, and False otherwise. Base your code on the previous function.
20. Write a function called every_element_greater_than that takes a number x and a
numerical Python list as parameters and determines whether all elements are greater
than x.
21. Write a function called without_vowels that takes a string as a parameter and returns
a new string that is the result of taking out vowels.
22. Write a function that takes two lists of the same size and a position as parameters, and
creates two new lists that are the result of exchanging the numbers of both lists in the
selected position.
23. Write a function that takes a list and two positions as parameters, and swaps the order
of all elements between selected positions.
24. Write a function that takes a numerical list as a parameter and returns the largest
element. Use looping structures.
25. Write a function that takes a numerical list and a number as parameters and
determines whether the number is found in the list. Use looping structures.
26. Write a function that takes a list of numbers and a number as parameters and returns
a new list with those elements lower than or equal to the number passed as argument.
27. Write a function that takes two lists as parameters and calculates a new list as the
intersection of elements in both lists.
28. Write a function that takes two numerical lists as parameters and returns the list
whose sum is greater.
29. Write a function that takes two numerical lists and returns a new list that
concatenates both lists.
30. Write a function that takes a text as a parameter and returns a dictionary with the
vowel-count.
31. Write a function called die_throw that takes the number of sides as parameter and
returns the results of throwing a die with that many sides.
32. Write a function called euclidean_distance that takes two tuples with x and y
coordinates as parameter and returns the Euclidean distance of both coordinates.
33. Write a function called get_closest_enemy_index that takes as a parameter a tuple
with the x and y coordinates of a character, and a Python list that contains tuples with
the x and y coordinates of enemies. The function should return list position of the
closest enemy using the Euclidean distance. Base your code on the previous function.
34. Write a function called swap_with_minimum that takes as a parameter a list position
and a list. The function will modify the list by swapping the value found in the position
passed as parameter with the minimum value found between the specified position
and the last position of the list.
35. Write a function called sort_array that sorts a numerical array, passed as parameter,
from lower to higher values. Base your code on the previous function.

You might also like