0% found this document useful (0 votes)
35 views10 pages

Day-1 Solutions

Python questions and Solutions

Uploaded by

Sai Rupa
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)
35 views10 pages

Day-1 Solutions

Python questions and Solutions

Uploaded by

Sai Rupa
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/ 10

Here are the solutions to the Python questions:

1. Very Basic Questions

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"

2. Find the length of a string without `len()`.


Solution :
text = "python"
count = 0
for char in text:
count += 1
print(count)
# Output: 6
```

3. Convert a string to uppercase and lowercase.


Solution
text = "python"
print(text.upper())
print(text.lower())
# Output: "", ""
```

4. Count the occurrences of a character in a string.


Solution
text = "banana"
count = text.count("a")
print(count)
# Output: 3
```
5. Check if a string starts and ends with a specific character.
Solution
text = "hello"
print(text.startswith("h"))
print(text.endswith("o"))
# Output: True, True
s = "Hello, World!"
substring = s[:5] # Slices the string from the start to index 5 (not inclusive of index 5)
print(substring)
```

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("A is Smaller") elif


b <= a and b <= c:

print("B is Samller")
else:

print("C is Smaller")

Output:

Enter A:13

Enter B:25

Enter C: 7

C is Smaller

4. Write a program to check if a number is positive, negative, or zero.

Solution:

num = int(input("Enter a number: "))

if num > 0:

print(f"{num} is positive.")

elif num < 0:

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 = []

for num in range(1500, 2701):

if num % 7 == 0 and num % 5 == 0:

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,

2415, 2450, 2485, 2520, 2555, 2590, 2625, 2660, 2695]

6. Write a Python program to convert temperatures to and from Celsius and Fahrenheit.
Solution:

temp = float(input("Enter temperature: "))

unit = input("Convert to (F)ahrenheit or (C)elsius? ").strip().upper()

if unit == "F":

print(f"{temp}°C = {temp * 9/5 + 32:.2f}°F")

elif unit == "C":


print(f"{temp}°F = {(temp - 32) * 5/9:.2f}°C")

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

Convert to (F)ahrenheit or (C)elsius? c


35.0°F = 1.67°

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:

number = int(input("Enter a positive integer: "))


sum_of_digits = 0
while number > 0:
sum_of_digits += number % 10
number //= 10
print("The sum of the digits is:", sum_of_digits)
Output:
Enter a positive integer: 24
The sum of the digits is: 6
3. Write a Python program that takes an integer n as input and prints the first numbers in
the Fibonacci series.
Solution:
n = int(input("Enter the number of terms: "))
a, b = 0, 1
if n <= 0:
print("Please enter a positive integer.")
elif n == 1:
print("Fibonacci series: 0")
else:
print("Fibonacci series:", end=" ")
print(a, b, end=" ")
for _ in range(2, n):
c=a+b
print(c, end=" ")
a, b = b, c
Output:
Enter the number of terms: 5
Fibonacci series: 0 1 1 2 3

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

10. Write a program to reverse a given string using a while loop.


Solution:
input_string = input("Enter a string: ")
reversed_string = ""
index = len(input_string) - 1
while index >= 0:
reversed_string += input_string[index]
index -= 1
print("Reversed string:", reversed_string)
Output:
Enter a string: Mahendra
Reversed string: ardnehaM

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

print("a is Type of", type(a)) #Prints type of variable a


print("b is Type of", type(b)) # Prints type of variable b
print("c is Type of", type(c)) #Prints type of variable c
```

# Output:
```
a is Type of <class 'int'>
b is Type of <class 'float'>
c is Type of <class 'complex'>
```

2. Write a program to take an Integer Input and Print Its Type

```
user_input = int(input("Please enter an integer: "))
print(type(user_input))
```

# Output:
```
Please enter an integer: 12
<class 'int'>
```

3.Write a program to reverse Tuple Elements

```
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[::-1])
```

# Output:
```
(5, 4, 3, 2, 1)
```

You might also like