-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathproblem_3.py
39 lines (32 loc) · 1.49 KB
/
problem_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 24 11:39:16 2025
@author: Somaia
--- 3: Exploring and Converting Between Variable Types in Python ---
So far, you’ve worked with integers, and now it’s time to explore more data types in Python, including float, str, and bool. Additionally, Python allows you to convert (or "cast") between different data types when needed.
For example:
- Converting a float to an integer: int(1.5) becomes 1.
- Converting a boolean to a string: str(True) becomes "True".
- Converting a string containing a number to a float: float("3.14") becomes 3.14.
Let’s put this into practice by working with variables and casting between types.
Instructions
1. Create a variable half as a float with the value 0.5.
2. Create a string variable intro with the value "Hello! How are you?".
3. Create a boolean variable is_good and assign it the value True.
4. Convert half to an integer and store the result in a new variable, half_int.
5. Convert is_good to a string and store it in a variable, is_good_str.
6. Combine intro and is_good_str using string concatenation, and assign the result to a new variable, message.
7. Print the values of half_int, is_good_str, and message.
"""
half = 0.5
intro = "Hello! How are you?"
is_good = True
# Perform type conversions
half_int = int(half)
is_good_str = str(is_good)
# Combine variables into a message
message = intro + " " + is_good_str
# Print results
print("half_int:", half_int)
print("is_good_str:", is_good_str)
print("message:", message)