COMPUTER SCIENCE
TOPIC- DATA TYPES
Q1. String input & length
Code:
s = input("Enter a string: ")
print("Length of string:", len(s))
Example Output:
Enter a string: Python
Length of string: 6
Q2. Accept integer, float & string and show types
Code:
x = int(input("Enter an integer: "))
y = float(input("Enter a float: "))
z = input("Enter a string: ")
print(type(x))
print(type(y))
print(type(z))
Example Output:
Enter an integer: 5
Enter a float: 3.14
Enter a string: Hello
<class 'int'>
<class 'float'>
<class 'str'>
1
COMPUTER SCIENCE
Q3. List of numbers (max & min)
Code:
nums = [12, 45, 3, 67, 23]
print("Max:", max(nums))
print("Min:", min(nums))
Example Output:
Max: 67
Min: 3
Q4. Student dictionary
Code:
student = {"Name": "Amogh", "Age": 17, "Marks": 88}
print(student)
Example Output:
{'Name': 'Amogh', 'Age': 17, 'Marks': 88}
Q5. Tuple of fruits
Code:
fruits = ("Apple", "Banana", "Mango", "Grapes", "Orange")
print("3rd fruit:", fruits[2])
Example Output:
3rd fruit: Mango
2
COMPUTER SCIENCE
TOPIC- OPERATORS
Q1. Arithmetic operations
Code:
a = 10
b=3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Modulus:", a % b)
print("Power:", a ** b)
Example Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus: 1
Power: 1000
Q2. Greater than 10 and even
Code:
n = 12
if n > 10 and n % 2 == 0:
print("Number is greater than 10 and even")
else:
print("Condition not satisfied")
Example Output:
Number is greater than 10 and even
3
COMPUTER SCIENCE
Q3. Bitwise operators
Code:
x, y = 5, 3
print("AND:", x & y)
print("OR:", x | y)
print("XOR:", x ^ y)
Example Output:
AND: 1
OR: 7
XOR: 6
Q4. Compare two strings
Code:
s1 = "hello"
s2 = "hello"
print("Equal?", s1 == s2)
Example Output:
Equal? True
4
COMPUTER SCIENCE
Q5. Divisible by 3 or 5
Code:
n = 15
if n % 3 == 0 or n % 5 == 0:
print("Divisible by 3 or 5")
else:
print("Not divisible by 3 or 5")
Example Output:
Divisible by 3 or 5
5
COMPUTER SCIENCE
TOPIC- IDENTIFIERS
Q1. Different naming styles
Code:
snake_case = "Hello"
camelCase = "World"
PascalCase = "Python"
print(snake_case, camelCase, PascalCase)
Example Output:
Hello World Python
Q2. Valid vs invalid identifier
Code:
my_var = 10 # valid
# 2var = 20 invalid
print(my_var)
Example Output:
10
Q3. Constants using uppercase
Code:
PI = 3.14
GRAVITY = 9.8
print(PI, GRAVITY)
Example Output:
3.14 9.8
6
COMPUTER SCIENCE
Q4. Function as identifier
Code:
def myFunction():
print("This is a function")
myFunction()
Example Output:
This is a function
7
COMPUTER SCIENCE
TOPIC- MULTI-LINE & MULTIPLE STATEMENTS
Q1. Multi-line statement with \
Code:
total = 1 + 2 + 3 + \
4+5
print("Sum:", total)
Example Output:
Sum: 15
Q2. Multiple statements in one line
Code:
a = 10; b = 20; print("Sum =", a + b)
Example Output:
Sum = 30
Q3. Area of rectangle
Code:
l, w = 5, 3; area = l * w; print("Area:", area)
Example Output:
Area: 15
Q4. Multi-line string
Code:
para = """Python is easy to learn.
It is powerful.
8
COMPUTER SCIENCE
It is beginner-friendly."""
print(para)
Example Output:
Python is easy to learn.
It is powerful.
It is beginner-friendly.
9
COMPUTER SCIENCE
TOPIC- COMMENTS
Q1. Single-line comments
Code:
# This program adds two numbers
a = 10
b = 20
print(a + b)
Example Output:
30
Q2. Multi-line comments
Code:
"""
This program calculates the square of a number.
It takes input and prints result.
"""
n=4
print("Square:", n ** 2)
Example Output:
Square: 16
Q3. Mix comments with code
Code:
n = 7 # number to check
is_prime = True
for i in range(2, n):
if n % i == 0: # if divisible, not prime
is_prime = False
break
print("Prime?", is_prime)
Example Output:
10
COMPUTER SCIENCE
Prime? True
Q4. Comment out code
Code:
# print("This will not run")
print("This will run")
Example Output:
This will run
11
COMPUTER SCIENCE
TOPIC- CONDITIONAL STATEMENTS
Q1. Even or Odd
Code:
n=8
print("Even" if n % 2 == 0 else "Odd")
Example Output:
Even
Q2. Voting Eligibility
Code:
age = 16
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
Example Output:
Not Eligible
Q3. Largest of two numbers
Code:
a, b = 10, 20
print("Largest:", a if a > b else b)
Example Output:
Largest: 20
Q4. Largest of three numbers
Code:
12
COMPUTER SCIENCE
a, b, c = 10, 25, 15
print("Largest:", max(a, b, c))
Example Output:
Largest: 25
Q5. Positive, Negative, Zero
Code:
n = -3
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
Example Output:
Negative
Q6. Leap Year
Code:
year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not Leap Year")
Example Output:
Leap Year
Q7. Grading System
Code:
marks = 82
if marks >= 90:
grade = "A"
13
COMPUTER SCIENCE
elif marks >= 75:
grade = "B"
elif marks >= 50:
grade = "C"
else:
grade = "Fail"
print("Grade:", grade)
Example Output:
Grade: B
Q8. Vowel or Consonant
Code:
ch = "a"
if ch in "aeiou":
print("Vowel")
else:
print("Consonant")
Example Output:
Vowel
Q9. Divisible by 3 and 7
Code:
n = 21
if n % 3 == 0 and n % 7 == 0:
print("Divisible by 3 and 7")
else:
print("Not Divisible")
Example Output:
Divisible by 3 and 7
Q10. Palindrome Number
Code:
14
COMPUTER SCIENCE
n = 121
if str(n) == str(n)[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Example Output:
Palindrome
15
COMPUTER SCIENCE
TOPIC- LOOPING STATEMENTS
Q1. Print 1–10
Code:
for i in range(1, 11):
print(i, end=" ")
Example Output:
1 2 3 4 5 6 7 8 9 10
Q2. Multiplication table of 5
Code:
for i in range(1, 11):
print("5 x", i, "=", 5 * i)
Example Output:
5x1=5
5 x 2 = 10
...
5 x 10 = 50
Q3. Sum of first 10 natural numbers
Code:
total = 0
for i in range(1, 11):
total += i
print("Sum =", total)
Example Output:
Sum = 55
16
COMPUTER SCIENCE
Q4. Factorial
Code:
n=5
fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial =", fact)
Example Output:
Factorial = 120
Q5. Even numbers (1–50)
Code:
for i in range(2, 51, 2):
print(i, end=" ")
Example Output:
2 4 6 ... 50
Q6. Odd numbers (1–50)
Code:
for i in range(1, 51, 2):
print(i, end=" ")
Example Output:
1 3 5 ... 49
17
COMPUTER SCIENCE
Q7. Reverse of a number
Code:
n = 1234
rev = 0
while n > 0:
rev = rev * 10 + n % 10
n //= 10
print("Reverse =", rev)
Example Output:
Reverse = 4321
Q8. Print digits separately
Code:
n = "456"
for d in n:
print(d)
Example Output:
4
5
6
Q9. Sum of digits
Code:
n = "456"
print("Sum of digits =", sum(int(d) for d in n))
Example Output:
Sum of digits = 15
18
COMPUTER SCIENCE
Q10. Fibonacci series
Code:
a, b = 0, 1
for _ in range(10):
print(a, end=" ")
a, b = b, a + b
Example Output:
0 1 1 2 3 5 8 13 21 34
19
COMPUTER SCIENCE
TOPIC- Nested Loops
Q1. Square pattern
Code:
for i in range(5):
print("* " * 5)
Example Output:
*****
*****
*****
*****
*****
Q2. Right triangle
Code:
for i in range(1, 6):
print("* " * i)
Example Output:
*
**
***
****
*****
20
COMPUTER SCIENCE
Q3. Inverted triangle
Code:
for i in range(5, 0, -1):
print("* " * i)
Example Output:
*****
****
***
**
*
Q4. Multiplication tables (2–5)
Code:
for i in range(2, 6):
for j in range(1, 6):
print(f"{i} x {j} = {i*j}")
print()
Example Output:
2 x 1 = 2 ... 2 x 5 = 10
...
5 x 5 = 25
21
COMPUTER SCIENCE
Q5. Number pattern
Code:
for i in range(1, 6):
for j in range(1, i+1):
print(j, end="")
print()
Example Output:
1
12
123
1234
12345
Q6. Pyramid pattern
Code:
for i in range(1, 6):
print(" " * (5-i) + "* " * i)
Example Output:
*
**
***
****
*****
22
COMPUTER SCIENCE
Q7. Hollow square
Code:
n=5
for i in range(n):
for j in range(n):
if i==0 or i==n-1 or j==0 or j==n-1:
print("*", end=" ")
else:
print(" ", end=" ")
print()
Example Output:
*****
* *
* *
* *
*****
Q8. Prime numbers (1–20)
Code:
for num in range(2, 21):
prime = True
for i in range(2, int(num**0.5)+1):
if num % i == 0:
prime = False
break
if prime:
print(num, end=" ")
Example Output:
2 3 5 7 11 13 17 19
23
COMPUTER SCIENCE
TOPIC- ALL PAIRS (I, J)
Code:
for i in range(1, 4):
for j in range(1, 4):
print(f"({i},{j})", end=" ")
print()
Example Output:
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
Q10. Checkerboard pattern
Code:
n=4
for i in range(n):
for j in range(n):
if (i+j) % 2 == 0:
print("X", end=" ")
else:
print("O", end=" ")
print()
Example Output:
XOXO
OXOX
XOXO
OXOX
24