top of page

Python Introduction Notes

 

1. What is Python?

• Python is a popular programming language created by Guido van Rossum in 1991.

• It is known for its simplicity, readability, and versatility.

• It supports multiple programming paradigms, such as procedural, object-oriented, and functional programming.

 

2. Key Features of Python:

• Easy to Read and Write: Python syntax is designed to be intuitive and similar to English.

• Interpreted Language: Python code is executed line by line, making it easier to debug.

• Cross-Platform: Python can run on different operating systems like Windows, macOS, and Linux.

• Extensive Libraries: Includes libraries for various purposes like web development, data analysis, and machine learning.

 

3. Python Syntax Basics:

• Case Sensitivity: Python is case-sensitive, meaning Var and var are different variables.

• Indentation: Python uses indentation (spaces or tabs) to define blocks of code. Example:
if 5 > 2:
    print("Five is greater than two!")


• Comments: Use # for single-line comments.

# This is a comment
print("Hello, World!")


4. Variables in Python:

• Creating Variables: No need to declare the type. Python infers it automatically.

x = 5          # integer
y = "Hello"    # string


 

• Variable Naming Rules:

• Must start with a letter or underscore _.

• Cannot start with a number.

• Can contain alphanumeric characters and underscores (a-z, 0-9, _).

 

5. Data Types in Python:

• Common Data Types:

• int - Integer numbers (e.g., 5, -1)

• float - Decimal numbers (e.g., 5.5, -2.3)

• str - Strings (e.g., "Hello", 'World')

• bool - Boolean (True, False)

 

6. Print Statement:

• Used to display output in Python.

• Example:
print("Hello, Python!")
print(5 + 3)


 

7. Example Code:

• Basic Python Program:

# This program prints a greeting
name = "Aman"
print("Hello, " + name + "! Welcome to Python.")


 

5 Multiple-Choice Questions (MCQs):

 

1. What is Python?

• a) A web browser

• b) A programming language

• c) A database management system

• d) A type of operating system


Answer: b) A programming language

 

2. Which of the following is a valid variable name in Python?

• a) 2ndValue

• b) value#1

• c) _value

• d) @value

 

Answer: c) _value

 

3. What symbol is used for single-line comments in Python?

• a) //

• b) /* */

• c) #

• d) <!-- -->

​

Answer: c) #

 

4. Which data type is used to store decimal numbers in Python?

• a) int

• b) float

• c) str

• d) bool

​

Answer: b) float

​

5. What will be the output of the following code?

print("5" + "3")

• a) 8

• b) 53

• c) 5 3

• d) Error

​

Answer: b) 53
 

Python Comments Notes

 

1. What are Comments in Python?

 

• Comments are used to explain Python code and make it more readable.

• They help others understand the purpose and logic behind the code.

• Comments are not executed when the program runs.

 

2. Types of Comments:

 

• Single-line Comments:

• Created using the # symbol.

• Everything after # on that line is ignored by Python.

• Example:
# This is a single-line comment
print("Hello, World!")

​

• Multi-line Comments:

• Python does not have a dedicated syntax for multi-line comments like other languages.

• However, multi-line comments can be created using multiple # symbols or triple quotes (''' or """).

​

• Example using multiple #:
# This is a comment
# that spans
# multiple lines
print("Hello, World!")

 

• Example using triple quotes:

"""
This is a multi-line comment.
It can span several lines.
"""
print("Hello, World!")

​

3. Why Use Comments?

 

• Improving Code Readability: Comments make the code easier to understand, especially for complex logic.

• Debugging: Comments can be used to temporarily disable parts of the code for debugging.

• Documentation: Helps in documenting what certain sections of the code do, especially in larger projects.

 

4. Best Practices for Writing Comments:

 

• Keep comments short and to the point.

• Use comments to explain why something is done, not how.

• Avoid obvious comments, such as:

x = 5  # Assign 5 to x

• Focus on adding comments that provide insights or reasoning behind the code.

 

5. Example Code with Comments:

# This function adds two numbers
def add_numbers(a, b):
    # Return the sum of a and b
    return a + b

# Calling the function
result = add_numbers(2, 3)
print(result)  # Output: 5


 

5 Multiple-Choice Questions (MCQs):

 

1. Which symbol is used to write a single-line comment in Python?

• a) //

• b) /* */

• c) #

• d) <!-- -->

​

Answer: c) #

​

2. How can you write multi-line comments in Python?

• a) Using /* ... */

• b) Using // ... //

• c) Using triple quotes (''' or """)

• d) Using <!-- ... -->

​

Answer: c) Using triple quotes (''' or """)

​

3. Why are comments used in Python?

• a) To execute code faster

• b) To make code unreadable

• c) To explain and document the code

• d) To store temporary data

​

Answer: c) To explain and document the code

​

4. Which of the following is a valid multi-line comment in Python?

• a) /* This is a comment */

• b) // This is a comment

• c) """ This is a comment """

• d) <-- This is a comment -->

​

Answer: c) """ This is a comment """

​

5. What will the following code output?
# print("This line is commented")
print("Hello, World!")

• a) This line is commented

• b) Hello, World!

• c) # print("This line is commented")

• d) Error

​

Answer: b) Hello, World!

 

​

bottom of page