0% found this document useful (0 votes)
2 views

Questions and Answers on Python Data Types

The document provides a series of questions and answers related to Python programming, covering common data types, variable declaration, and control flow statements such as if-else, for loops, and while loops. It includes examples for each concept, demonstrating how to use Python's data types, write conditional statements, and implement loops. The content serves as a basic guide for understanding fundamental Python programming concepts.

Uploaded by

b5mj96y2wp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Questions and Answers on Python Data Types

The document provides a series of questions and answers related to Python programming, covering common data types, variable declaration, and control flow statements such as if-else, for loops, and while loops. It includes examples for each concept, demonstrating how to use Python's data types, write conditional statements, and implement loops. The content serves as a basic guide for understanding fundamental Python programming concepts.

Uploaded by

b5mj96y2wp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Questions and Answers on Python Data Types, Variables, If-Else Statements, For Loops, and While

Loops

1. Question: What are Python’s common data types, and provide examples for each?
Answer: Python's common data types include:

o int: Integer values (e.g., 5)

o float: Decimal values (e.g., 3.14)

o str: Text or string data (e.g., "hello")

o list: A collection of elements, mutable (e.g., [1, 2, 3])

o tuple: A collection of elements, immutable (e.g., (1, 2, 3))

o dict: A collection of key-value pairs (e.g., {'a': 1, 'b': 2})

2. Question: How do you declare and use variables in Python?


Answer: You can declare variables in Python without specifying their type, as it is dynamically
typed. For example:

x = 10 # Integer

y = 3.14 # Float

name = "John" # String

print(x, y, name)

3. Question: What is the difference between list, tuple, and set in Python?
Answer:

o list: Ordered, mutable, and allows duplicates. Example: [1, 2, 2, 3]

o tuple: Ordered, immutable, and allows duplicates. Example: (1, 2, 2, 3)

o set: Unordered, mutable, and does not allow duplicates. Example: {1, 2, 3}

4. Question: Write a program using if-else to check if a number is positive, negative, or zero.
Answer:

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

if num > 0:

print("Positive")

elif num < 0:

print("Negative")

else:

print("Zero")
5. Question: How does the range() function work in a for loop? Provide an example.
Answer: The range() function generates a sequence of numbers. Example:

for i in range(1, 11):

print(i)

6. Question: Write a program using a while loop to print all even numbers between 1 and 20.
Answer:

num = 2

while num <= 20:

print(num)

num += 2

7. Question: Write a program using if-else to assign grades based on a score.


Answer:

score = int(input("Enter your score: "))

if 90 <= score <= 100:

print("Grade: A")

elif 80 <= score < 90:

print("Grade: B")

elif 70 <= score < 80:

print("Grade: C")

elif 60 <= score < 70:

print("Grade: D")

else:

print("Grade: F")

8. Question: Write a for loop to iterate over a list and print strings with more than 5 characters.
Answer:

words = ["apple", "banana", "kiwi", "strawberry"]

for word in words:

if len(word) > 5:

print(word)
9. Question: Write a while loop to keep asking for a number until the user enters -1.
Answer:

while True:

num = int(input("Enter a number (-1 to stop): "))

if num == -1:

break

10. Question: Write a program to print numbers from 1 to 50 with special rules for multiples of 3
and 5.
Answer:

for i in range(1, 51):

if i % 3 == 0 and i % 5 == 0:

print("FizzBuzz")

elif i % 3 == 0:

print("Fizz")

elif i % 5 == 0:

print("Buzz")

else:

print(i)

You might also like