0% found this document useful (0 votes)
2 views

Programming with Python

The document provides an overview of Python programming, covering its definition, features, and setup instructions. It includes sections on conditional statements, looping, string manipulation, lists, tuples, dictionaries, functions, modules, file handling, and exception handling, with examples for each topic. Key concepts such as data types, syntax, and methods are highlighted to aid in understanding Python basics.

Uploaded by

ytsubscribers.sg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming with Python

The document provides an overview of Python programming, covering its definition, features, and setup instructions. It includes sections on conditional statements, looping, string manipulation, lists, tuples, dictionaries, functions, modules, file handling, and exception handling, with examples for each topic. Key concepts such as data types, syntax, and methods are highlighted to aid in understanding Python basics.

Uploaded by

ytsubscribers.sg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Programming with Python: Basics

1. What is Python, and why is it popular?


o Python is a high-level, interpreted programming language
known for its simplicity and readability. Its popularity
comes from its versatility, extensive libraries, and active
community support.
2. Who developed Python, and when?
o Python was developed by Guido van Rossum in 1991.
3. What are some key features of Python?
o Easy to learn and use, interpreted language, platform-
independent, supports object-oriented programming, has
extensive libraries, and is open-source.
4. How do you set up Python on your system?
o Download and install Python from python.org. Add
Python to the system PATH during installation for easy
execution from the command line.
5. What are the basic data types in Python?
o Integer, Float, String, Boolean, List, Tuple, Dictionary, and
Set.

Conditional Statements
6. What is the syntax for an if-else statement in Python?
python
CopyEdit
if condition:
# code block
else:
# another code block
7. Can you nest if-else statements in Python? Provide an
example.
o Yes, nested if-else statements are allowed.
python
CopyEdit
if x > 0:
if x % 2 == 0:
print("Positive Even")
else:
print("Positive Odd")
else:
print("Non-positive")

Looping
8. What are the differences between for and while loops in
Python?
o A for loop is used when the number of iterations is known
beforehand (e.g., iterating over a list). A while loop is used
when the number of iterations depends on a condition.
9. What is a nested loop? Provide an example.
o A loop inside another loop is called a nested loop.
python
CopyEdit
for i in range(3):
for j in range(3):
print(i, j)
10. What does the break statement do in a loop?
o It terminates the current loop prematurely.

String Manipulation
11. How do you access individual characters in a string?
o Using indexing: string[index]
Example:
python
CopyEdit
s = "Python"
print(s[0]) # Output: P
12. What is string slicing?
o Slicing extracts a part of the string.
python
CopyEdit
s = "Python"
print(s[1:4]) # Output: yth
13. Name some string methods.
o upper(), lower(), strip(), split(), join(), find(), replace().

Lists
14. How do you declare a list in Python?
o Using square brackets:
python
CopyEdit
my_list = [1, 2, 3]
15. What are some common list methods?
o append(), extend(), insert(), remove(), pop(), sort(),
reverse().
16. How can you access elements in a list?
o Using indexing and slicing:
python
CopyEdit
my_list = [10, 20, 30]
print(my_list[1]) # Output: 20

Tuples
17. What is the difference between a list and a tuple?
o A list is mutable (modifiable), while a tuple is immutable
(cannot be modified).
18. How can you access elements in a tuple?
o Using indexing and slicing, similar to lists.

Dictionaries
19. What is a dictionary in Python?
o A dictionary is a collection of key-value pairs.
Example:
python
CopyEdit
my_dict = {"name": "John", "age": 30}
20. How do you access values in a dictionary?
o Using keys:
python
CopyEdit
print(my_dict["name"]) # Output: John

Functions
21. What are the types of functions in Python?
o Built-in functions and user-defined functions.
22. What is an anonymous function?
o A function defined using the lambda keyword.
python
CopyEdit
add = lambda x, y: x + y
print(add(2, 3)) # Output: 5
23. What are function arguments?
o Arguments can be positional, keyword, default, or
variable-length.
Modules
24. How do you import a module in Python?
o Using the import keyword.
python
CopyEdit
import math
print(math.sqrt(16))
25. What is the difference between a module and a
package?
o A module is a single file, while a package is a collection of
modules organized in directories.

File Handling
26. How do you open a file in Python?
o Using the open() function:
python
CopyEdit
file = open("example.txt", "r")
27. What are the different file modes in Python?
o r (read), w (write), a (append), rb (read binary), etc.
28. How do you read and write data to a file?
o Use read() or write().
Example:
python
CopyEdit
with open("example.txt", "w") as file:
file.write("Hello, World!")

Exception Handling
29. What is exception handling in Python?
o A mechanism to handle runtime errors using try, except,
finally.
30. Can you define a custom exception?
o Yes, by inheriting from the Exception class.
python
CopyEdit
class MyError(Exception):
pass

You might also like