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

TP n1 Python

Uploaded by

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

TP n1 Python

Uploaded by

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

ENSA-Kenitra Module : Compétence Numériques

LAB 2 : Python First Steps


1. First Python commands

Instruction to type Result obtained + comment or explanation


20+1 21

20/3 6.666666666666667

20//3 6

20%3 2

5.45*10 54.5

2**4 16

(3+2)*5 25 the priority is given to the parentheses

3+2*5 13 the priority is given to the multiplication

result = 3 + 5 * 2 ** 3 - 4 / 2 the priority is given to the power then the


41
print(result) multiplication

result = (10 + 5) * 3 - 8 / 2 ** 2 + 1 the priority is given to the parentheses


44 then the power then the multiplication
print(result)

2. Data Types

Instruction to type Result obtained + comment or explanation


type(3) <class 'int'> it is an integer

type(1.5) <class 'float'> it is a float

type(3.0) <class 'float'> it is a float

type("bonjour") <class 'str'> it is a string

type("3") <class 'str'> it is a string

type(3+1.5) <class 'float'> it is a float

a = 10 it is an integer
<class 'int'>
print(type(a))

Pr. Mehdia AJANA Chapter2 – Python First Steps 1


ENSA-Kenitra Module : Compétence Numériques

b = 3.14 <class 'float'> it is a float


print(type(b))
c = True <class 'bool'> it is a boolean value
print(type(c))

d = "Python" <class 'str'> it is a string


print(type(d))
a = input("Enter your Enter your the first instruction asks the user for their height
height: ") height: 178 then it is saved in the variable a
print(type(a)) <class 'str'> a if non indicated is automatically saved as a string
a = float(input("Enter Enter your height: 178 same as the previous question but this
your height: ")) <class 'float'> time we have indicated the type of the
print(type(a)) variable

Exercises :

1. Write a script that asks the user for the day, month, and year, then displays the date as
follows:
Today’s Date is : 12/12/2023.

2. Describe what happens in each of the three lines of the example below:
>>> width = 20
>>>height = 5 * 9.3
>>>width * height
930.0
3. Assign the respective values 3, 5, and 7 to three variables a, b, c.
Perform the operation a - b // c.
Interpret the obtained result.

4. Change your script by asking the user to enter the three variables’ values, and then
display their types and the operation result.

5. Write a Python program that takes a student's score as input and outputs the
corresponding grade based on the following grading scale:
Score ≥ 90: Grade A
Score 80-89: Grade B
Score 70-79: Grade C
Score 60-69: Grade D
Score < 60: Grade F

Ensure that the input is valid (i.e., the score should be between 0 and 100).
If the input is invalid, print an error message like "Invalid score!"

Pr. Mehdia AJANA Chapter2 – Python First Steps 2


ENSA-Kenitra Module : Compétence Numériques

6. Weather Decision System: Write a Python program that asks the user to input the
current weather conditions and decides whether a person should go for a walk based on
the following:

 If it's raining, the person should stay indoors.


 If it's windy but not raining, the person should only go for a walk if the
temperature is above 20°C. Otherwise, they should stay indoors.
 If it's neither raining nor windy, the person should go for a walk if the
temperature is above 10°C. Otherwise, they should stay indoors because it's too
cold.

Instructions:

 Ask the user for weather input:


o is it raining? (True/False)?
o is it windy? (True/False)
o What is the temperature?

Example Output:

 "Stay indoors, it's raining."


 "Go for a walk, it's windy but warm."
 "Go for a walk, the weather is fine."
 …

Pr. Mehdia AJANA Chapter2 – Python First Steps 3

You might also like