Subject: Computer Science (Skill Enhancement Course)
Semester: 2nd Semester (FYUG)
Exam Name: Even Semester Exam, 2024
Python is widely used due to its simple syntax that mimics natural language, its vast library support (like NumPy and Pandas), and its versatility across domains such as Web Development, Data Science, and AI
.One major application is Data Science and Machine Learning, where Python is used for data analysis, visualization, and building predictive models
.Single-line comments are written using the hash symbol (#)
''' or """).
print(2**3+(5+6)**(1+1))Calculation: 23 + 112 = 8 + 121 = 129
.It is a conditional branching statement used to execute different blocks of code based on multiple conditions
.elif stands for "else if".
Indentation refers to the spaces or tabs at the beginning of a code line
. In Python, it is used to define the scope or block of code (like in loops or functions), whereas other languages use curly braces.str = 'Hello World!'; print(str[2:5])This is a string slicing operation
. It takes characters from index 2 to 4 (5 is excluded). Output:llo.
A list is an ordered, mutable collection of items that can be of different data types, enclosed in square brackets []
a=[1,2,3]; a=tuple(a); a[0]=2; print(a)Output: TypeError
. Explanation: The lista is converted to a tuple. Tuples are immutable, so assigning a[0]=2 is not allowed.
Recursion is a programming technique where a function calls itself directly or indirectly to solve a smaller sub-problem of the same type
.
a=3; b=1
print(a, b) # Output: 3 1
a, b = b, a
print(a, b) # Output: 1 3
The open() function is used to open a file
file_object = open("filename", "mode").
Day=["Sun", "Mon", "Tue", "Wed"]; del Day[2]; print(Day)Output: ['Sunday', 'Monday', 'Wednesday']
Tokens are the smallest individual units in a Python program
. Examples: Keywords (if), Identifiers (variable_name), Literals (10), and Operators (+).
if)while, for)Mutability means the content can be changed after creation
. In a dictionary, you can add, modify, or delete key-value pairs.Example: d = {'a': 1}; d['a'] = 2 changes the value to 2.
print(), len(), max()def keywordUse the pyplot module from matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
factorial = factorial * i
print("The factorial of", num, "is", factorial)
nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence:", n1)
else:
while count < nterms:
print(n1, end=" ")
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
if (a >= b) and (a >= c):
largest = a
elif (b >= a) and (b >= c):
largest = b
else:
largest = c
print("The largest number is", largest)
File handling involves four primary steps
:open() with modes like 'r' (read), 'w' (write), 'a' (append)read(), readline() to extract data, or write() to add dataclose() to free up system resources