PYTHON1
PYTHON1
Answer:
Python is a high-level, interpreted programming language known for its simplicity and readability.
Python emphasizes code readability and allows programmers to express concepts in fewer lines of
code.
1b. Name 5 of the features of python and write short note on them.
Answer:
i. Easy to read and learn: Python's syntax is known for being clear and concise,
resembling natural language more than some other programming languages.
ii. Free and Open-Source: Python is an open-source language, meaning its development
is freely available for anyone to contribute to.
iii. Extensive Libraries: Python boasts a vast collection of standard libraries and third-
party packages. These libraries provide pre-written code for various tasks like data
analysis (NumPy, Pandas), web development (Django, Flask), machine learning
(TensorFlow, PyTorch), and more.
iv. Interpreted Language: Unlike compiled languages, Python code doesn't need to be
translated into machine code before execution. Instead, an interpreter reads and
executes the code line by line, making development faster and more flexible
v. Object-Oriented Programming (OOP): Python supports OOP concepts like classes,
objects, inheritance, and polymorphism.
1c. Write a python programming to print out your name, age, sex, matriculation number
and year of admission
Answer:
age = 20
sex = "Male"
matriculation_number = "12345678"
year_of_admission = 2022
print("Name:", name)
print("Age:", age)
print("Sex:", sex)
QUESTION TWO
2a. List the two types of comments in python and write short note on each of them
Answer:
1. Single-Line Comments: Single-line comments in Python are created using the hash
symbol (#). Any text following the # on the same line is ignored by the Python interpreter.
Single-line comments are typically used for brief explanations or annotations within the
code.
2. Multi-Line Comments (Block Comments): Multi-line comments, or block comments,
are used to comment out multiple lines of code or to write longer comments that span
several lines. In Python, multi-line comments are often created using triple quotes (''' or
"""). Although this syntax is technically for multi-line strings, it is commonly used for
block comments when the content is not assigned to a variable.
2b. What are the values for the following python expression?
Answer:
i. 2**(3**2) = 512
ii. (2**3)**2 = 64
iii. 2**3**2 = 512
iv. 8/4/2 = 1.0
v. 8/(4/2) = 4.0
Answer:
2d. State the differences between object oriented programming and procedure oriented
programming
Answer:
3a. List the operators in python, and state their order of precedence
Answer:
1. Arithmetic Operators:
Exponentiation (**): Computes the power of a number.
Multiplication (*), Division (/), Floor Division (//), Modulo (%): Perform multiplication,
division, integer division, and modulo operation respectively.
Addition (+), Subtraction (-): Perform addition and subtraction.
2. Comparison Operators:
Greater Than (>), Greater Than or Equal To (>=), Less Than (<), Less Than or Equal To
(<=): Compare the values of two operands.
Equality (==), Inequality (!=): Check for equality and inequality between operands.
3. Logical Operators:
Logical NOT (not): Returns the opposite boolean value of the operand.
4. Assignment Operators:
Compound Assignment (+=, -=, *=, /=, %=, //=, **=): Perform arithmetic operation and
assign the result to the variable.
5. Identity Operators:
Identity (is): Returns True if both operands refer to the same object.
Non-identity (is not): Returns True if both operands do not refer to the same object.
3b. write a python programming to compute the sum and average of ten numbers
Answer:
total_sum = 0
count = 10
for i in range(count):
total_sum += num
Answer:
4a. list and explain five of the supported data types in python, with examples
Answer:
1. Integer (int): Integers represent whole numbers, both positive and negative, without any
decimal point. Examples are 1,2,3,4,5.
2. Float (float): Floats represent real numbers with a decimal point. They can represent
fractional values. Examples are 2.3, 4.01.
3. String (str): Strings represent sequences of characters enclosed within single quotes (') or
double quotes ("). Strings are immutable in Python. Examples are “lola”, “hello”
4. Boolean (bool): Booleans represent the truth values True and False, which are used in
logical expressions and conditional statements.
5. List (list): Lists are ordered collections of items, which can be of mixed data types. Lists
are mutable, meaning their elements can be changed after creation.
Answer:
4c. List five of the control structures and loops, and write on any two with relevant example
Answer:
1. Conditional Statements:
if, elif, else: Used to perform different actions based on different conditions.
2. Loops:
for loop: Used to iterate over a sequence (e.g., list, tuple, string) or other iterable
objects.
while loop: Used to repeatedly execute a block of code as long as a specified condition
is true.
break statement: Used to exit the loop prematurely.
continue statement: Used to skip the rest of the code inside a loop for the current
iteration and continue to the next iteration.
QUESTION FIVE
5a. List the four types of inheritance and write short note on each of them
Answer:
Answer:
ELSE Statement:
Purpose: The else statement is used to define a block of code that executes when the if
condition evaluates to False.
Usage: The else statement comes after the if block and before any elif blocks (if present).
It does not take any condition, as it is the default block to execute when the preceding if
condition is not satisfied.
Execution: The else block is executed only if none of the preceding if or elif conditions
evaluate to True.
ELIF Statement:
Purpose: The elif statement is short for "else if" and is used to define additional conditions
to check after the initial if condition. It allows for the evaluation of multiple conditions in
sequence.
Usage: The elif statement comes after the initial if statement and before the optional else
statement. It takes a condition that is evaluated only if the preceding if condition is not
satisfied.
Execution: If the preceding if condition is not satisfied, the elif condition is evaluated. If
the elif condition evaluates to True, the corresponding block of code is executed. If the elif
condition is not satisfied, the next elif (if any) is evaluated, and so on.
Answer:
1. Ease of Use.
2. Broad Database Support.
3. Integration Capabilities.
4. Abundance of Libraries and Tools.
5. Cross-Platform Compatibility.