Day-1 Solutions
Day-1 Solutions
1. Concatenate a list of words ["Python", "is", "fun"] into a single sentence and print it.
```
words = ["Python", "is", "fun"]
sentence = " ".join(words) # Joins the list of words with a space between them
print(sentence)
```
Output:
```
Python is fun
```
---
2. Write a Python program to convert a string containing a number (e.g., "123") into an
integer.
```
string_number = "123"
integer_number = int(string_number) # Converts string to integer
print(integer_number)
```
Output:
```
123
```
---
3. Convert a floating-point number (e.g., 12.34) into an integer and print the result.
```
float_number = 12.34
integer_number = int(float_number) # Converts float to integer (removes the decimal part)
print(integer_number)
```
Output: 12
```
---
4. Given a string `s = "Hello, World!"`, write a program to extract the substring "Hello" by
using slicing in Python.
Solution :
s = "Hello, World!"
substring = s[:5] # Slices the string from the start to index 5 (not
inclusive of index 5)
print(substring)
Output:
Copy code
Hello
2. Strings
```1.Write a program to reverse a string.
Solution :
text = "hello"
reversed_text = text[::-1]
print(reversed_text)
# Output: "olleh"
Output:
```
Hello
```
These solutions cover string manipulation, data type conversion, and slicing techniques in
Python, which are important for basic programming practice.
3. Conditional Statements
1. Python program to check whether the person is eligible to vote or not?
Solution:
n=int(input("enter person age: "))
if n >=18:
print("eligible to vote")
else:
print("not eligible")
Output:
enter person age: 12
not eligible
enter person age: 25
eligible to vote
2. Write a Python program to find the biggest number among three numbers.
Solution:
a= int(input("Enter A:"))
b= int(input("Enter B:"))
c= int(input("Enter C: "))
if a >= b and a >= c:
print("A is bigger")
elif b >= a and b >= c:
print("B is bigger")
else:
print("C is bigger")
Output:
Enter A:12
Enter B:20
Enter C: 17
B is bigger
3. Write a Python program to find the smallest number among three numbers.
Solution:
a= int(input("Enter A:"))
b= int(input("Enter B:"))
c= int(input("Enter C: ")) if
a <= b and a <= c:
print("B is Samller")
else:
print("C is Smaller")
Output:
Enter A:13
Enter B:25
Enter C: 7
C is Smaller
Solution:
if num > 0:
print(f"{num} is positive.")
print(f"{num} is negative.")
else:
print(f"{num} is zero.")
Output:
Enter a number: 12 12
is positive.
Enter a number: 12 12
is positive.
Enter a number: 0
0 is zero.
5. Write a Python program to find those numbers which are divisible by 7 and multiples
of 5, between 1500 and 2700?
Solution:
numbers = []
numbers.append(num)
print(numbers)
Output:
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820, 1855, 1890, 1925,
1960, 1995, 2030, 2065, 2100, 2135, 2170, 2205, 2240, 2275, 2310, 2345, 2380,
6. Write a Python program to convert temperatures to and from Celsius and Fahrenheit.
Solution:
if unit == "F":
else:
print("Invalid choice.")
Output:
Enter temperature: 35
Convert to (F)ahrenheit or (C)elsius? f
35.0°C = 95.00°F
Enter temperature: 35
4.Loops
1. Write a Python program that prints the multiplication table of a number entered by the
user, from 1 to 10.
Solution:
number = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
Output:
Enter a number: 7
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
2. Write a Python program that accepts a positive integer from the user and uses a while
loop to calculate the sum of its digits.
Solution:
4. Write a Python program that takes an integer input and reverses the digits using a
while loop.
Solution:
number = int(input("Enter an integer: "))
reversed_number = 0
is_negative = number < 0
if is_negative:
number = -number
while number > 0:
digit = number % 10
reversed_number = reversed_number * 10 + digit
number //= 10
if is_negative:
reversed_number = -reversed_number
print("Reversed number:", reversed_number)
Output:
Enter an integer: 12
Reversed number: 21
5. Python program to perform sum of n numbers (n=10) by using loop.
Solution:
n=10
sum = 0
for i in range(1, n + 1):
sum += i
print("The sum of the first", n, "numbers is:", sum)
Output:
The sum of the first 10 numbers is: 55.
6. Python program to generate the prime numbers from 1 to N Solution: without using
function:
Solution:
N = int(input("Enter the value of N: "))
if N < 2:
print("No prime numbers in this range.")
else:
print("Prime numbers from 1 to", N, "are:", end=" ")
for num in range(2, N + 1):
is_prime = True
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=" ")
Output:
Enter the value of N: 5
Prime numbers from 1 to 5 are: 2 3 5
7. Python program to calculate the product of all elements in a list.
Solution:
numbers = [2, 3, 4]
product = 1
for num in numbers:
product *= num # Multiply each number to product
print("Product of numbers:", product)
Output:
Product of numbers: 24
8. Python program to print the even numbers from 1 to 20
Solution:
for number in range(1, 21):
if number % 2 == 0:
print(number)
Output:
2 4 6 8 10 12 14 16 18 20
9. Create a Python program that uses a while loop to print all odd numbers between 1
and 10?
Solution:
number = 1
while number <= 10:
if number % 2 != 0:
print(number)
number += 1
Output:
13579
5.Data types
1. Write a program to demonstrate Different Number Data Types in Python.
```
a = 10 Integer #Datatype
b = 11.5 Float # Datatype
c = 2.05j Complex #Number
# Output:
```
a is Type of <class 'int'>
b is Type of <class 'float'>
c is Type of <class 'complex'>
```
```
user_input = int(input("Please enter an integer: "))
print(type(user_input))
```
# Output:
```
Please enter an integer: 12
<class 'int'>
```
```
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[::-1])
```
# Output:
```
(5, 4, 3, 2, 1)
```