0% found this document useful (0 votes)
9 views10 pages

Python Basics: Features, Data Types & Control Statements

Uploaded by

tatheutkarsha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Python Basics: Features, Data Types & Control Statements

Uploaded by

tatheutkarsha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🔹 Chapter 1: Introduction to Python – 3 Marks Answers

1. Features of Python

Python is a high-level, interpreted, object-oriented programming language.

Its important features are:

Simple & Easy: Syntax is very clean and easy to learn.

Interpreted: Code executes line-by-line, making debugging easier.

Object Oriented: Supports class, objects, inheritance, polymorphism.

Portable: Same code can run on Windows, Mac, Linux without changes.

Large Libraries: Comes with huge standard libraries for file handling, maths,
GUI, web etc.

2. Standard Data Types

Python madhye mainly 5 standard data types astat:

Numbers: int, float, complex

String: Characters enclosed in quotes

List: Ordered & mutable collection ([])

Tuple: Ordered but immutable collection (())

Dictionary: Key–value pairs ({})

Set: Unordered unique elements ({})

3. Variables & Variable Declaration

Variable means memory location to store data.

Python madhye variable declare karayla datatype lihava lagat nahi.

Example:

Copy code

Python

X = 10

Name = “Utkarsha”
4. Constants & Literals

Constant: Value which should not change. (Python does not enforce, but
capital letters use karatat)

Example: PI = 3.14

Literal: Fixed value in code (numbers, string, boolean).

Example: 10, “hello”, True.

5. Comments

Comments program madhye explain karayla vapratat.

Single line comment: #

Multi-line: triple quotes.

6. Input / Output Functions

Input() → user kadun value ghete

Print() →

📌 Chapter 2 — Control Statements (3 Marks Paragraph Answers)

1️⃣Types of Control Statements

Control statements are used in Python to control the flow of program


execution. They help decide which part of the code should run and how
many times it should run. There are three main types of control statements:
selection statements, looping statements, and jump statements. Selection
statements like if, if-else, and elif allow the program to take decisions based
on conditions. Looping statements such as for and while help in repeating a
block of code multiple times. Jump statements like break, continue, and pass
are used to alter the normal flow of loops. Together, these control statements
make programs flexible, logical, and interactive.

2️⃣Type Conversion

Type conversion refers to changing the datatype of a value from one form to
another. Python supports two types of conversions: implicit and explicit.
Implicit conversion is done automatically by Python when a smaller datatype
is converted into a larger one, such as converting an integer to a float during
arithmetic operations. Explicit conversion, also known as type casting, is
performed manually by using functions like int(), float(), str(), and list().
These functions allow programmers to convert values into the desired
datatype when needed. Type conversion is important for handling user input,
arithmetic operations, and data processing.

3️⃣String Formatting Operators

String formatting allows programmers to display text in a well-structured and


readable way. Python provides several formatting techniques. The %
operator Is one of the oldest methods and works like placeholders to insert
values into a string. Another method is the format() function, where {}
braces are used as placeholders and values are inserted dynamically. The
latest and most convenient method is the f-string, introduced in Python 3.6,
which allows variables to be embedded directly inside the string using {}.
String formatting is useful for printing calculated results, generating bills,
reports, and user-friendly output.

4️⃣Loop Control Statements

Loop control statements are used to manage the execution of loops in


Python. There are three main loop control statements: break, continue, and
pass. The break statement immediately stops the loop even if its condition is
still true. The continue statement skips the remaining code inside the loop
for the current iteration and moves to the next iteration. The pass statement
does nothing; it acts as a placeholder when a statement is required but no
action is needed. These control statements provide better control over loops
and help in writing efficient programs.

📌 Chapter 3 — List, Functions, Tuple, Dictionary & Sets (3 Marks Paragraph


Answers)

1️⃣List (Definition + Uses)

A list in Python is an ordered, mutable collection of elements. It can store


different datatypes such as integers, strings, and even other lists. Lists are
created using square brackets []. Since lists are mutable, we can add, delete,
or modify elements anytime. Python provides many built-in list methods like
append(), insert(), remove(), pop(), and sort() which make list operations
very easy. Lists are widely used in programs to store and process large
amounts of data efficiently.

2️⃣Update and Delete Elements in List


Python allows modifying list elements because lists are mutable. Updating an
element is done by directly assigning a new value using its index, such as
list[0] = 10. Deleting elements can be done using several methods. The
remove() method deletes a specific value, while the pop() method removes
an element at a given index. The del statement can delete a single element
or even the entire list. These features make lists flexible and suitable for data
manipulation tasks.

3️⃣Local and Global Variables (Difference)

A local variable is a variable declared inside a function and can be accessed


only within that function. It is temporary and is destroyed once the function
ends. A global variable is declared outside all functions and can be accessed
by any function in the program. If a function wants to modify a global
variable, it must use the global keyword. Local variables provide function-
level data protection, while global variables allow sharing data across
functions. This distinction helps in structuring programs properly.

4️⃣Anonymous Function (Lambda Function)

An anonymous function in Python is a function without a name, created using


the lambda keyword. These functions are small and are used when a simple
operation needs to be performed only once. They can take multiple
arguments but contain only one expression. Lambda functions are often used
with functions like map(), filter(), and sorted() to perform quick operations.
Their short and compact nature makes them useful for short-term
calculations and functional programming.

5️⃣Difference Between List and Tuple

Lists and tuples both store ordered collections of elements, but they differ in
mutability. Lists are mutable, meaning their elements can be changed after
creation, while tuples are immutable and cannot be modified. Lists use
square brackets [] and are slower because they allow modifications. Tuples
use parentheses () and are faster due to their fixed structure. Tuples are used
for fixed data like coordinates, while lists are used when frequent
modifications are required.

6️⃣Set Operations

A set in Python is an unordered collection of unique elements. Sets support


mathematical operations like union, intersection, difference, and symmetric
difference. Union combines elements from both sets, intersection gives
common elements, and difference gives elements present in one set but not
in the other. Sets are useful for removing duplicates and performing fast
membership testing.

📌 Chapter 4 — Modules, Files & Exception Handling (3 Marks Answers)

1️⃣Module

A module in Python is a file that contains functions, variables, and classes


that can be reused in other programs. It helps in organizing large code into
smaller, manageable parts. Python has many built-in modules such as math,
random, and datetime. We can import these modules using the import
keyword. Modules improve code reusability and reduce repetition by allowing
programmers to use pre-written functions.

2️⃣Package & How to Create/Import Package

A package is a collection of related modules stored in a directory. It must


contain a special file named __init__.py to be recognized as a package. To
create a package, we make a folder and place modules inside it with an
__init__.py file. To use the package, we import it using the import
package_name or from package_name import module_name statement.
Packages help in organizing large projects and avoiding name conflicts.

3️⃣User-Defined Package

A user-defined package is a package created by the programmer to manage


their own modules. It follows the same structure as a normal package but
contains user-created Python files. By placing related modules in one
package, it becomes easier to maintain and reuse code across different
programs. User-defined packages are helpful in building structured
applications.

4️⃣File Handling (Read & Write)

File handling in Python allows a program to store data permanently. Files can
be opened using the open() function with modes like “r” for reading, “w” for
writing, and “a” for appending. The read(), readline(), and write() methods
help in performing operations on files. After performing operations, the file
must be closed using close(). File handling is essential for applications like
data processing, reports, and saving user information.

5️⃣Regular Expression

Regular expressions (regex) are patterns used to search and manipulate


strings. Python provides the re module for working with regex. They help find
specific patterns such as phone numbers, emails, and special formats within
text. Functions like search(), match(), and findall() make pattern matching
easy. Regular expressions are powerful tools in data validation and text
processing.

6️⃣Exception Handling

Exception handling is used to manage runtime errors in Python. Errors such


as dividing by zero or accessing invalid indexes stop program execution.
Python provides the try-except block to handle such errors gracefully. Code
that may cause an error is written inside try, and the handling code is written
inside except. Optional blocks like else and finally offer additional control.
Exception handling prevents program crashes and makes applications more
reliable.

✅ PYTHON IMPORTANT 1 & 2 MARK QUESTIONS (

⭐ 1 MARK QUESTIONS (MOST REPEATED)

1. What is indentation in Python?

Indentation is the space before a statement in Python. It defines code blocks


and is mandatory.

2. What is a variable?

A variable is a name used to store a value in memory.

3. What is a literal?

A literal is a fixed value written directly in the code, like 10, 5.2, “hello”.

4. What is a list?

A list is a mutable, ordered collection of elements.

5. What is a tuple?

A tuple is an immutable, ordered collection of elements.

6. What is a set?

A set is an unordered collection of unique elements.

7. What is a dictionary?

A dictionary stores data in key–value pairs.


8. What is a module?

A module is a Python file containing functions, variables, and classes.

9. What is a package?

A package is a collection of modules stored in a directory containing


__init__.py.

10. What is an exception?

An exception is an error that occurs during program execution.

11. Write any one loop control statement.

Break, continue, or pass.

12. What does the break statement do?

It stops the loop immediately.

13. What is a lambda function?

A small anonymous function defined using the lambda keyword.

14. What is type casting?

Converting one data type to another using functions like int(), float(), str().

15. What is the use of print()?

It displays output on the screen.

⭐ 2 MARK QUESTIONS (WITH SHORT PARAGRAPH ANSWERS)

1) Define indentation with example.

Indentation in Python refers to the spaces used at the beginning of a line to


define a block of code. Python uses indentation to group statements under
loops, conditionals, and functions. Example:

Copy code

Python

If a > 5:

Print(“Greater”)

2) Explain type conversion.


Type conversion means changing one data type into another. Python
supports two types: Implicit conversion, which is done automatically by
Python (e.g., int + float → float). Explicit conversion is done manually using
functions like int(), float(), and str().

3) Difference between list and tuple.

A list is mutable, meaning its elements can be changed, added, or removed.


It uses square brackets []. A tuple is immutable, meaning it cannot be
modified after creation, and uses parentheses ().

4) What is a dictionary? Give example.

A dictionary is a key-value data structure in Python. Each value is accessed


using its key. Example:

Copy code

Python

Student = {“name”:”Amit”, “age”:20}

5) Explain break and continue statements.

The break statement terminates the loop completely.

The continue statement skips the current iteration and moves to the next
iteration.

6) What is a module? How do you import it?

A module is a Python file containing functions and variables that can be


reused. It is imported using:

Copy code

Python

Import module_name

7) What is file handling?

File handling allows reading and writing data to files. We use open() with
modes like “r” (read), “w” (write), and “a” (append). After the operation, the
file is closed using close().

8) What is an exception? How is it handled?


An exception is a runtime error that stops program execution. It is handled
using try-except blocks to prevent the program from crashing.

9) What are set operations?

Python sets support operations like union, intersection, difference, and


symmetric difference, used for mathematical set operations.

10) What is a lambda function? Give example.

A lambda function is a small anonymous function defined using the keyword


lambda. Example:

Copy code

Python

Add = lambda x, y

1. What is dry run in Python?

Dry run means executing the program manually, line-by-line, without actually
running it on the computer. It helps to predict the output, understand logic
flow, and identify errors before execution.

2. What is type conversion?

Type conversion means changing the data type of a value into another type,
such as converting integer to float or string to integer using functions like
int(), float(), or str().

3. What is the use of pass statement?

Pass is a null statement used when a statement is syntactically required but


you do not want to execute any code. It helps in creating empty loops, empty
functions, or future code blocks.

4. What is enumerate()?

Enumerate() is a built-in function that returns both index and value while
iterating over a sequence. It simplifies loops by avoiding manual index
counters.

5. What is an identifier?

An identifier is the name given to variables, functions, classes, or modules. It


must start with a letter or underscore and cannot contain spaces or special
characters.
1. Explain any two metacharacters in Regular Expressions.

Metacharacters are special characters used in regex patterns.

^ is used to match the start of a string, while $ matches the end of a string.
These help in creating precise and powerful pattern-matching rules.

2. Explain backward indexing in strings.

Backward indexing means accessing characters of a string using negative


indices. For example, -1 refers to the last character, -2 refers to second last,
and so on. It allows easy access to elements from the end of the string.

3. Explain extend() method of list.

The extend() method adds multiple elements from another list or iterable to
the end of an existing list. It modifies the original list and works like
concatenation.

You might also like