Python Exercises and Solutions Guide
Python Exercises and Solutions Guide
ScientificComputing version2.2
Python3
Corrected exercises
Statements
Statements of exercises
Remark
The following exercises are provided as examples and models.
They are either simple or less simple (rated◃ in the margin) may be difficult (noted◃◃).
1. Enter a float. If it is positive or zero, display its root; otherwise, display a message.
of error.
5. Write a filtered input of an integer in the interval from 1 to 10, inclusive. Display
the seizure.
11. The clause loops. In this exercise, perform the inputs with some
and the displays with some , both belonging to the module .
Initialize a list with 5 integers of your choice and then input an integer.
In a for loop, iterate through the list. If the entered integer belongs to the list, save it.
and interrupt the loop (since you found it). If the loop completed successfully,
use a clause to display a message announcing it.
Now enter another integer, this time positive.
Write a loop to determine if this integer is prime. If it is not,
the loop should display the first divisor found and stop. If it is prime,
to display it in a clause .
6. Write another function with three arguments, and which returns their sum.
In the main program, define a tuple of three numbers, then use the syn-
call tax for the function that decompresses the tuple. Display the result.
2. Initialize like an empty list, and like a list of five null floats.
Display these lists.
Use the function to display:
the integers from 0 to 3;
the integers from 4 to 7;
the integers from 2 to 8 in steps of 2.
Define like a list of integers from 0 to 5 and test the membership of the elements
items 3 and 6 to .
7. Define two sets: X= {a,b,c,d} and Y= {s,b,d}, then display the results
following:
the initial sets;
the membership test of the element àX;
the membership test of the element àY;
the setsX− YetY− X;
the set X∪ Y(union) ;
the set X∩ Y(intersection).
9. The dictionary type (or associative array) allows representing structured arrays.
Indeed, each key is associated with a value in a dictionary, and this value can
itself be a data structure (list, tuple or a dictionary...).
Te /Tf2970 1063
At
Z/A79 196.967
Te Tf2237 29.8
Go
Z/A31 69.72
Assign the data from this table to a dictionary in a way that allows
write for example:
11. In the same way, implement a FIFO queue with a list. Try to add▹
a queue manipulation menu.
Advice: Only use procedures without arguments and a list as a global variable.
Definitions:
We call a prime number any natural number greater than 1 that has exactly
two divisors, itself and one;
A proper divisor of n is any divisor of n, excluding n itself.
A natural number is said to be perfect if it is equal to the sum of all its proper divisors;
– numbers such as: (a+ n+ n 2 is prime for all such that 06n < (a− 1), are
called lucky numbers.
The test part must include four calls to the function allowing to test
, , and .
1. Write a module to calculate the roots of the real trinomial: ax2+ bx+ c.
The module defines a function with the three parameters of the trinomial, a, beta
c. The function must return a tuple whose first element is the number of roots
of the trinomial (0, 1 or 2), and the other elements are the possible roots.
Test your function with the following three sets of values: 1,−3, 2, 1−2.1 and 1.1,1.
In the main program, instantiate an object of the class and invoke the
method .
In the main program, instantiate two show them and show their
sum.
Define a class inheriting from and which overloads the instance attribute:
.
In the main program, instantiate a and one and display them.
Finally write a self-test that displays an instance of Segment initialized with the values
1, 2, 3 and 4.
In the main program, create the instance you desire through the factory.
then verify its type by calling the method on it .
Additional scripts
1. Write a program that, based on the input of a radius and a height, calculates the
volume of a right cone.
2. A while loop: enter a price excluding tax (enter 0 to finish) and display its total price including tax.
3. Another while loop: calculate the sum of a sequence of positive or zero numbers.
Count how many data points there were and how many were above 100.
A number less than or equal to 0 indicates the end of the sequence.
4. The user gives a positive integer and the program displays if it is divisible by
2 and otherwise.
5. The user provides a positive integer and the program announces how many times in a row
this integer is divisible by 2.
◃ The user provides an integer greater than 1 and the program displays, if there are any, all its
propressing divisors repeated as well as their number. If there are none, it indicates that it
is first. For example:
7. Write a program that estimates the value of the mathematical constant using
the formula:
n 1
e = ∑ i!
i =0
To do this, define the factorial function and, in your main program, input
the order shows the corresponding approximation of.
A lighthouse keeper goes to the bathroom five times a day but the restrooms are on the ground floor...
Write a procedure (thus without return) which receives two parameters
the number of steps of the lighthouse and the height of each step (in cm), and which displays:
10. I am tied to the tracks at the Arras station. Write a program that displays a table.
allowing me to know the time at which I will be shredded by the train that left from
the Gare du Nord at 9 am (there are 170 km between the Gare du Nord and Arras).
The table will predict the different possible times for all speeds of 100 km/h
at 300 km/h, in increments of 10 km/h, the results being rounded down to the nearest minute.
Write a procedure who receives the speed of the train and displays the time
of the drama;
Write the main program that displays the requested table.
11. A main program inputs a valid DNA strand and a DNA sequence will-▹
"Valid" means that they are not empty and are exclusively formed of a
arbitrary combination of , , or ).
Write a function which returns true if the input is valid, false otherwise.
Write a function who performs a valid entry and returns the entered value under
form of a string.
Write a function who receives two arguments, the string and the sequence and
which returns the proportion of sequence in the chain (i.e. its number of o-
currencies).
The main program calls the function for the chain and for the sequence
and display the result.
Sample display:
12. It is a matter of writing, on one hand, a main program, and on the other hand, a function to...
listed in the main program.
The user fills in a table of N= 100 integers with random integers using
saint a function a, b which returns an integer between a and b− A function
named receives this table and returns the index of the cell that contains
the minimum.
Write the function .
Write the program that swaps the first element of the array with the minimum.
this table.
13. A table has N= 100 floating variables, of which the first (n< 100)
are used.
Write a function which returns the index of the largest floating point among
cesn, and another which returns the index of the smallest.
Then write a main program that performs the following actions:
– filtered input (you must ensure that it cannot be entered outside of its
limits);
– random filling of the first values of the table (we will use the module ,
without arguments, which returns a float randomly between 0.0 and+1.0) ;
display of the amplitude of the table (difference between its largest and smallest)
their) ;
Display of the average of the first values of the table.
14. Write a function which receives two parameters, a temperature and an integer
, and which returns the Celsius conversion→ Fahrenheit (= 1), or Fahrenheit→ Celsius
( = 2).
Reminder: TF= 32+ 1.8× TC
16. Enter an integer between 1 and 3999 (why this limitation?). Display it in Roman numerals.
main.
◃ 18. A table contains integers (2< n< 100), all between 0 and 500. Verify that they
are all different.
[Link] user enters an integer between 2 and 12, the program gives the number of ways
of fairening launching two dice.
20. Same problem as the previous one but with between 3 and 18 and three dice.
◃◃ 21. Generalization of the two previous questions. The user enters two inputs, of a
first the number of dice, nbd (which we will practically limit to 10), and on the other hand the
sum, s, included between [Link]. The program calculates and displays the number
ways to do with the ideas.
23. To perform calculations on square matrices, we can use the "list of lists" type.
and index an element of the 3è line and 4è column parm[2][3] (considering that the
indices start at 0).
We declare three square matrices of dimension N: m1, m2, and m3 containing integers.
We affect them, line by line, by the N2the first even integers starting from 2; m2 is
the identity matrix, that is to say it contains 1s on the main diagonal (NW-SE)
and 0 everywhere else.
Practically, we will limit ourselves to Nmax.= 10. Write the algorithm for the calculation of:
m3= m1− m2