Seesional Sol
Seesional Sol
Solutions
Q1. In Python, variables store data values (e.g., strings, integers, floats) without
needing a type declaration. They’re named containers holding data for reuse or
manipulation. For example:
name = "Alice"
age = 25
print("Name:", name, "Age:", age)
This stores "Alice" in name and 25 in age.
Q2. The input() function in Python allows users to enter data during program
execution. It reads user input as a string, enabling interactive programs. For
example:
name = input("Enter your name: ")
print("Hello, " + name)
Q3. In Python, a local variable is defined within a function and accessible only
inside that function. A global variable is defined outside any function and
accessible throughout the program. For example:
x = 10 # global variable
def func():
y = 5 # local variable
Here, x is global, and y is local.
Q4. In Python, semicolons (;) are optional and not required to end statements, as
Python uses line breaks to separate them. If you add a semicolon at the end, it
won’t cause an error but is generally unnecessary. However, it’s used to write
multiple statements on one line.
# Continue example
for i in range(5):
if i == 3:
continue
print(i)
In the break example, the loop stops when i is 3; in the continue example, it
skips printing 3.
(b) # Input a number from the user
number = float(input("Enter a number: "))
greet()
Each block is defined by indentation, making the code visually structured and
Pythonic. Indentation errors will cause Python to raise an error.
Q3 (a) In Python, data types define the type of data a variable can hold. Python
supports a wide range of built-in data types, which can be categorized into basic
and advanced types. Here, we'll discuss four basic data types.
1. Integer (int)
Represents whole numbers, both positive and negative, without a decimal point.
x=5
print(type(x)) # Output: <class 'int'>
2. Float (float)
Represents numbers with a decimal point, used for more precise values.
python
Copy code
y = 3.14
print(type(y)) # Output: <class 'float'>
3. String (str)
Represents a sequence of characters, enclosed in single, double, or triple quotes.
python
Copy code
name = "Alice"
print(type(name)) # Output: <class 'str'>
4. Boolean (bool)
Represents truth values: True or False.
python
Copy code
is_active = True
print(type(is_active)) # Output: <class 'bool'>
These basic data types form the foundation of variable storage and manipulation
in Python programs.
(b) # Input a number from the user
number = int(input("Enter a number: "))
# Initialize variables
factorial = 1
i=1
if is_prime:
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
(b)
def count_case_letters(input_string):
# Initialize counters for uppercase and lowercase letters
uppercase_count = 0
lowercase_count = 0
# Example usage
input_string = input("Enter a string: ")
upper, lower = count_case_letters(input_string)
print(f"Uppercase letters: {upper}")
print(f"Lowercase letters: {lower}")
Q5 (a)
Explanation:
1. is_palindrome: The function converts the number into a string and
compares it with its reverse using slicing ([::-1]).
2. If the number is the same as its reverse, it is a palindrome.
3. The program accepts user input and checks whether the number is a
palindrome or not.
Example:
For the input 121, the program will output: 121 is a palindrome.
For the input 123, the program will output: 123 is not a palindrome.