0% found this document useful (0 votes)
8 views4 pages

Python Basics

The document outlines an assignment consisting of 15 questions related to Python programming, covering topics such as features of Python, types of operators, data types and structures, operator precedence, conditional statements, looping statements, and exception handling. It includes examples for each concept, comparisons between Python and C, and methods for list manipulation. The assignment aims to assess understanding of Python programming fundamentals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Python Basics

The document outlines an assignment consisting of 15 questions related to Python programming, covering topics such as features of Python, types of operators, data types and structures, operator precedence, conditional statements, looping statements, and exception handling. It includes examples for each concept, comparisons between Python and C, and methods for list manipulation. The assignment aims to assess understanding of Python programming fundamentals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment No: 1

(Answer any Ten)

Q1. List and explain different features of Python programming language.

1. Interpreted Language: Python is executed line by line, making debugging easier.


2. Cross-Platform: Python can run on various operating systems such as Windows,
MacOS, and Linux.
3. Extensive Libraries: Python has a vast standard library and many third-party
libraries.
4. Dynamically Typed: Variables in Python do not need an explicit declaration to
reserve memory space.
5. High-Level Language: Python abstracts low-level details like memory management.
6. Object-Oriented: Python supports object-oriented programming, allowing
encapsulation, inheritance, and polymorphism.
7. Open Source: Python is free to use and distribute, including for commercial
purposes.
8. Integrated: Python can be easily integrated with other languages like C, C++, and
Java.

Q2. List out the different types of operators available in Python programming language.
Illustrate each with a suitable example.

1. Arithmetic Operators: +, -, *, /, %, **, //


o Example: 5 + 3 = 8
2. Comparison Operators: ==, !=, >, <, >=, <=
o Example: 5 > 3 = True
3. Logical Operators: and, or, not
o Example: (5 > 3) and (3 < 8) = True
4. Bitwise Operators: &, |, ^, ~, <<, >>
o Example: 5 & 3 = 1
5. Assignment Operators: =, +=, -=, *=, /=, %=, **=, //=
o Example: a = 5; a += 3 -> a = 8
6. Identity Operators: is, is not
o Example: 5 is 5 = True
7. Membership Operators: in, not in
o Example: 'a' in 'apple' = True

Q3. Explain different Data types and Data structures available in Python.

1. Data Types:
o int: Integer values.
o float: Floating-point values.
o str: String values.
o bool: Boolean values (True or False).
o complex: Complex numbers.
2. Data Structures:
o List: Ordered, mutable collection. e.g., [1, 2, 3]
o Tuple: Ordered, immutable collection. e.g., (1, 2, 3)
o Set: Unordered, mutable collection of unique elements. e.g., {1, 2, 3}
o Dictionary: Unordered collection of key-value pairs. e.g., {'a': 1, 'b':
2}

Q4. Give the importance of operator precedence and associativity rules. Evaluate the
following expression by applying the operator precedence and associativity rules For
a=5, b=3, c=12, d=10 and e=3

Operator precedence and associativity determine the order in which operations are performed
in an expression. This ensures expressions are evaluated correctly.

1. a + b ** (c // d) * e
o c // d => 12 // 10 => 1
o b ** 1 => 3 ** 1 => 3
o 3 * e => 3 * 3 => 9
o a + 9 => 5 + 9 => 14
o Result: 14
2. a * (e + c) - d / b
o e + c => 3 + 12 => 15
o a * 15 => 5 * 15 => 75
o d / b => 10 / 3 => 3.333...
o 75 - 3.333... => 71.666...
o Result: 71.666...

Q5. Write a note on the following methods of Python programming language:

1. input(): Reads a line from input, converts it into a string.


o Example: name = input("Enter your name: ")
2. type(): Returns the type of an object.
o Example: type(5) => <class 'int'>
3. id(): Returns the unique id of an object.
o Example: id(5)
4. ord(): Returns the Unicode code of a character.
o Example: ord('a') => 97

Q6. Compare and contrast C and Python.

• Syntax: Python has simpler, more readable syntax than C.


• Typing: Python is dynamically typed, while C is statically typed.
• Speed: C is faster due to closer hardware-level operations.
• Memory Management: C requires manual memory management; Python has
automatic garbage collection.
• Use Cases: C is often used for system programming; Python is preferred for web
development, data analysis, and scripting.

Q7. Describe the different types of conditional statements of Python programming


language with suitable examples.

1. if statement: Executes a block of code if the condition is true.


o Example: if a > b: print("a is greater")
2. if-else statement: Executes a block of code if the condition is true, otherwise another
block of code.
o Example: if a > b: print("a is greater") else: print("b is
greater")
3. elif statement: Checks multiple expressions for true and executes a block of code as
soon as one condition is true.
o Example: if a > b: print("a is greater") elif a < b: print("b is
greater") else: print("both are equal")

Q8. Describe the different types of looping statements of Python programming language
with suitable examples.

1. for loop: Iterates over a sequence (list, tuple, string).


o Example: for i in range(5): print(i)
2. while loop: Repeats as long as the condition is true.
o Example: while a > 0: print(a); a -= 1

Q9. Differentiate between break and continue.

• break: Exits the nearest enclosing loop.


o Example: for i in range(5): if i == 3: break
• continue: Skips the current iteration and continues with the next iteration.
o Example: for i in range(5): if i == 3: continue; print(i)

Q10. Define list. List out the important features of the list and demonstrate how to add
an element to the list using the + operator, append(), and insert() methods.

• List: An ordered, mutable collection of items.


• Features: Mutable, allows duplicates, indexed.
• Adding Elements:
o + operator: list1 = [1, 2]; list2 = list1 + [3]
o append(): list1.append(3)
o insert(): list1.insert(1, 3)

Q11. Explain the importance of the following list methods:

1. remove(): Removes the first occurrence of a value.


o list1.remove(3)
2. index(): Returns the index of the first occurrence of a value.
o list1.index(3)
3. sum(): Returns the sum of all elements.
o sum(list1)
4. count(): Returns the number of occurrences of a value.
o list1.count(3)
5. extend(): Adds all elements of a list to another list.
o list1.extend([4, 5])
6. reverse(): Reverses the list in place.
o list1.reverse()
7. sort(): Sorts the list in ascending order.
o list1.sort()
Q12. Write a Note on Local and Global Variables.

• Local Variables: Defined inside a function, accessible only within that function.
• Global Variables: Defined outside all functions, accessible throughout the program.
• Example:

python
x = "global"
def func():
x = "local"
print(x)
func() # Prints "local"
print(x) # Prints "global"

Q13. Explain Exception Handling with an example.

• Exception Handling: Mechanism to handle runtime errors, allowing the program to


continue execution.
• Example:

python
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Execution finished")

Q14. Compare List and Tuple.

• Mutability: Lists are mutable, tuples are immutable.


• Syntax: Lists use square brackets [], tuples use parentheses ().
• Performance: Tuples are faster than lists.
• Usage: Use tuples for fixed collections of items, lists for dynamic collections.

Q15. Explain any 4 string Handling Functions.

1. len(): Returns the length of the string.


o Example: len("hello") => 5
2. upper(): Converts the string to uppercase.
o Example: "hello".upper() => "HELLO"
3. lower(): Converts the string to lowercase.
o Example: "HELLO".lower() => "hello"
4. replace(): Replaces a substring with another substring.
o Example: "hello world".replace("world", "Python") => "hello
Python"

You might also like