-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
63 lines (48 loc) · 1.6 KB
/
Copy pathscript.py
File metadata and controls
63 lines (48 loc) · 1.6 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Question 1: Declare three variables of type int, float, and str,
# then print their types using type().
a = 10
b = 10.5
c = "kalpan"
print(type(a))
print(type(b))
print(type(c))
# Question 2: Write a program that takes two numbers as input,
# converts them to integers, and prints their sum, difference,
# product, and quotient.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)
print("Difference:", a - b)
print("Product:", a * b)
print("Quotient:", a / b)
# Question 3: Predict the output of the following code, then verify by running it:
# x = "5"
# y = 3
# print(x * y)
#
# Prediction: "555" (string multiplied by int repeats the string)
# Actual Output:
x = "5"
y = 3
print(x * y)
# Question 4: Write a program that checks whether a number entered by the user
# is positive, storing the result of the comparison in a boolean variable.
a = int(input("Enter a number: "))
if a > 0:
print("Number is positive")
is_pos = True
else:
print("Number is negative")
is_pos = False
# Question 5: Explain the difference between implicit and explicit type conversion,
# with one example of each.
#
# Implicit Type Conversion:
# Python automatically converts one type to another without user intervention.
# Example: num_int = 10, num_float = 2.5, result = num_int + num_float
# Here int is automatically converted to float, result = 12.5
#
# Explicit Type Conversion:
# User manually converts one type to another using functions like int(), float(), str().
# Example: num_str = "10", num_int = int(num_str)
# Here we manually convert string to integer.