Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python Programming Fundamentals: Operators, Data Types, OOP, and NumPy, Exams of Computer Science

A comprehensive overview of fundamental python programming concepts, including operators, data types, object-oriented programming principles, and the numpy library. It includes explanations, examples, and exercises to solidify understanding. Suitable for beginners and those seeking to strengthen their python programming skills.

Typology: Exams

2023/2024

Available from 01/25/2025

shraddha-kosare
shraddha-kosare 🇮🇳

2 documents

1 / 124

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PYDC
Q1 a) Explain the different type of operators in Python
with suitable examples.
>>
Types of Operators in Python:
Python provides several types of operators that allow for performing various
operations on variables and values. Here are the primary categories of
operators in Python:
1. Arithmetic Operators:
o These operators are used to perform mathematical operations
such as addition, subtraction, multiplication, etc.
o Examples:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus)
** (Exponentiation)
// (Floor Division)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Python Programming Fundamentals: Operators, Data Types, OOP, and NumPy and more Exams Computer Science in PDF only on Docsity!

PYDC

Q1 a) Explain the different type of operators in Python

with suitable examples.

Types of Operators in Python: Python provides several types of operators that allow for performing various operations on variables and values. Here are the primary categories of operators in Python:

  1. Arithmetic Operators: o These operators are used to perform mathematical operations such as addition, subtraction, multiplication, etc. o Examples: ▪ + (Addition) ▪ - (Subtraction) ▪ * (Multiplication) ▪ / (Division) ▪ % (Modulus) ▪ ** (Exponentiation) ▪ // (Floor Division)
  1. Comparison (Relational) Operators: o These operators compare two values and return a Boolean result (True or False). o Examples: ▪ == (Equal to) ▪ != (Not equal to) ▪ > (Greater than) ▪ < (Less than) ▪ >= (Greater than or equal to) ▪ <= (Less than or equal to)

▪ += (Add and assign) ▪ - = (Subtract and assign) ▪ *= (Multiply and assign) ▪ /= (Divide and assign)

  1. Membership Operators: o These operators are used to test if a sequence (like a list, tuple, or string) contains a specified value. o Examples: ▪ in (Returns True if a value exists in a sequence) ▪ not in (Returns True if a value does not exist in a sequence)
  2. Identity Operators: o These operators are used to compare the memory locations of two objects. o Examples: ▪ is (Returns True if both variables point to the same object)

▪ is not (Returns True if both variables do not point to the same object)

  1. Bitwise Operators: o These operators are used to perform operations on bits of integers. o Examples: ▪ & (Bitwise AND) ▪ | (Bitwise OR) ▪ ^ (Bitwise XOR) ▪ ~ (Bitwise NOT) ▪ << (Left shift) ▪ >> (Right shift)
  2. Ternary Operator (Conditional Expression):

Method Overloading

Method Overriding:

Q2 a) Build Python code to determine whether the

given Number is a Armstrong or Not

b) Explain the different types of Data types in Python

with examples.

  1. Numeric Types: o int: Integer numbers, e.g., 5 , - 10 o float: Floating-point numbers, e.g., 3.14, - 5. o complex: Complex numbers, e.g., 3+5j

Q3 a) Interpret the following terms -

i. Class iii. Polymorphism ii. Encapsulation

iv. Abstraction

i. Class:

  • A class is a template for creating objects.
  • It encapsulates data for the object and methods to manipulate that data.
  • It allows creating multiple objects with the same set of attributes and behaviors.
  • Example: ii. Encapsulation:
  • Encapsulation ensures that the internal state of an object is hidden from the outside.
  • Only the object’s methods can access or modify its internal state.
  • Encapsulation is achieved by making attributes private (using double underscore __).
  • It protects data from accidental modification and allows controlled access via getter/setter methods.
  • Example: iii. Polymorphism:
  • Polymorphism allows methods to work with objects of different types and perform differently depending on the object's class.
  • Method Overloading (same method name, different parameters) and Method Overriding (same method name, different class) are forms of polymorphism.
  • It simplifies code by enabling the same method to behave differently based on the object.
  • Example:
  • Example:

b) Develop a Python program to create a Student

class. Include attributes like name, class, Section with

the marks of 5 subject. Implement a method to

determine the Percentage of each student.

o Animal class with a method sound(). o Dog and Cat classes override the sound() method to produce different sounds. Lambda Function:

  • Definition : A small anonymous function that is defined using the lambda keyword.
  • Characteristics : o Anonymous : Lambda functions do not have a name. o Concise : Typically used for simple, one-line functions. o Single Expression : Contains a single expression, automatically returning the result of the expression. o Flexible Arguments : Can take multiple arguments.
  • Example : o lambda a, b: a + b creates a function that adds two numbers. Example Code:

c) Apply python code to create a class representing a

Rectangle. Include methods to calculate its area and

perimeter.