0% found this document useful (0 votes)
6 views4 pages

Lab2 PythonFirstSteps

The document provides an introduction to basic Python commands and data types, including arithmetic operations, variable assignments, and type checking. It includes exercises for users to practice input handling, conditional statements, and creating simple programs such as a grading system and a weather decision system. The content is aimed at helping learners understand fundamental programming concepts in Python.

Uploaded by

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

Lab2 PythonFirstSteps

The document provides an introduction to basic Python commands and data types, including arithmetic operations, variable assignments, and type checking. It includes exercises for users to practice input handling, conditional statements, and creating simple programs such as a grading system and a weather decision system. The content is aimed at helping learners understand fundamental programming concepts in Python.

Uploaded by

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

ENSA-Kenitra Module : Compétence Numériques

LAB 2 : Python First Steps


1. First Python commands

Instruction to type Result obtained + comment or explanation


21
20+1
#addition
6.666666666667
20/3
# Division that results in a float
6
20//3
# Integer division, discards the remainder
2
20%3
#gives the remainder of the division
54.5
5.45*10
# Multiplication of a float and an integer
16
2**4
# ** is the exponentiation operator
25
(3+2)*5
#Parentheses have the highest precedence, so 3 + 2 is
evaluated first
13
3+2*5
#Multiplication has higher precedence than addition
41.0
result = 3 + 5 * 2 ** 3 - 4 / 2
#2**3 is evaluated first
print(result)
44.0
result = (10 + 5) * 3 - 8 / 2 ** 2 + 1
#2**2 and (10+5) evaluated first
print(result)

2. Data Types

Instruction to type Result obtained + comment or explanation


<class 'int'>
type(3)
<class 'float'>
type(1.5)
<class 'float'>
type(3.0)

type("bonjour") <class 'str'>

<class 'str'>
type("3")
"3"enclosed in quotes, so it’s treated as a string.
<class 'float'>
type(3+1.5)
<class 'int'>
a = 10

print(type(a))

Pr. Mehdia AJANA Chapter2 – Python First Steps 1


ENSA-Kenitra Module : Compétence Numériques

b = 3.14 <class 'float'>


print(type(b))
<class 'bool'>
c = True #The variable c holds the Boolean value True.
print(type(c))
<class 'str'>
d = "Python"
print(type(d))
a = input("Enter your <class 'str'>
height: ") #The input() function collects user input and always returns a string,
print(type(a)) Even if the user types a number, it is treated as text.
a = float(input("Enter <class 'float'>
your height: ")) # float() used to convert the user’s input into a floating-point number.
print(type(a))

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.

day = input('entrer day jour: ')


month = input('entrer month: ')
year= input("entrer year: ")

print(f" Today’s Date is {day}/{month}/{year}")

2. Describe what happens in each of the three lines of the example below:
>>> width = 20 This line creates a variable named width and assigns it the value
of 20.
>>> height = 5 * 9.3 This line creates another variable named height and assigns it
the result of the expression 5 * 9.3

>>>width * height 930.0 this line calculates the product of width and height.
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.
a=3
b=5
c=7
result = a - b // c
>>> 3
Interpretation:
Floor Division (b // c) 5//7>>>0
Subtraction (a - (b // c)) 3-0= 3

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

Pr. Mehdia AJANA Chapter2 – Python First Steps 2


ENSA-Kenitra Module : Compétence Numériques

a = input("Enter the value for a: ")


b = input("Enter the value for b: ")
c = input("Enter the value for c: ")
result = a - (b // c)
print(f"The type of a is: {type(a)}")
print(f"The type of b is: {type(b)}")
print(f"The type of c is: {type(c)}")
print(f"The result of the operation a - b // c is: {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!"

score=float(input("enter the score: "))


if(score >= 0 and score<= 100):
if (score >= 90):
print("GRADE A")
elif (score <90 and score>= 80):
print("GRADE B")
elif (score <80 and score>= 70):
print("GRADE C")
elif (score <70 and score>= 60):
print("GRADE D")
else:
print("GRADE F")
else:
print("INVALID SCORE")

Pr. Mehdia AJANA Chapter2 – Python First Steps 3


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."
 …

is_raining = input("Is it raining? (True/False): ").lower() == 'true'


is_windy = input("Is it windy? (True/False): ").lower() == 'true'
temperature = float(input("What is the temperature in °C?: "))

if is_raining:
print("Stay indoors, it's raining.")
elif is_windy and temperature > 20:
print("Go for a walk, it's windy but warm.")
elif not is_windy and temperature > 10:
print("Go for a walk, the weather is fine.")
else:
print("Stay indoors, it's too cold.")

Pr. Mehdia AJANA Chapter2 – Python First Steps 4

You might also like