Python Task - Solved
➢Python Task
➢ Section 1: Python Basics & Syntax
Q: 1
➢ Output: Hello World
print("Hello World")
Q: 2
➢ The '#' symbol is used for comments in Python. Anything after it is ignored
by the interpreter.
Q: 3
➢ Strings in Python can be defined using single (' '), double (" "), or
triple quotes (''' ''' or ).
➢ Section 2: Variables and Data Types
Q: 4
➢ 1st_name = "Ali" is NOT valid because variable names cannot
start with a digit.
Q: 5
x = "5"
x = int(x)
# x will be 5 (an integer)
Q: 6
Four primitive data types: int, float, str, bool
➢ Section 3: Operators
Q:7
Output of 3 + 2 * 5 is 13 (multiplication happens before addition)
Q: 8
The % operator returns the remainder. Example: 10 % 3 = 1
➢ Section 4: Input/Output
Q: 10
This program asks for two floats and prints their sum
Example:
Input: 2.5 and 3.5 => Output: Sum: 6.0
x = float(input("First: "))
y = float(input("Second: "))
print("Sum:", x + y)
Q: 11
print "Hello" is invalid in Python 3. It must be print("Hello")
Q: 12
x = 10
y=3
print(x % y) # Output: 1
Q: 13
Name: str
Age: int
Logged in status: bool
Q: 14
age = int(input("Enter your age: "))
Q: 15
Use '#' for single-line comments
Q: 16
is_happy = True # This assigns the boolean value True to the variable
is_happy
Q: 17
area = length * width
Q: 18
x=5
x += 3
print(x) # Output: 8
Q: 19
Valid: user1, user_name
Invalid: 1user (starts with digit), user name (contains space)
Q: 20
name = input("What is your name?")
print("Hello", name)
➢ Section 5: Easy Programs
Q: 1. Greeting Program
name = input("What is your name? ")
print("Hello, " + name + "!")
#Q:2. Program for addition and multiplication
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("Sum:", num1 + num2)
print("Product:", num1 * num2)