Updated Python Notes
Updated Python Notes
This document contains a comprehensive overview of Python, covering its syntax, key
concepts, and examples to help you prepare for your exam.
1. Basics of Python
Python is an interpreted, high-level, general-purpose programming language.
Hello, World!
# Code Example:
print("Hello, World!")
Comments
# Single-line comment
# This is a comment
# Assigning Variables
x=5 # Integer
y = 3.14 # Float
z = "Hello" # String
# Dynamic Typing
a = 10
a = "Python" # 'a' now refers to a string
Data Types
3. Conditional Statements
# if-else Example
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
# if-elif-else Example
y = 20
if y < 10:
print("y is less than 10")
elif y == 20:
print("y is 20")
else:
print("y is greater than 10 and not 20")
4. Loops
For Loop
While Loop
# Function Syntax
def function_name(parameters):
# Function body
return value
# Example:
def greet(name):
return f"Hello, {name}!"
def introduce(self):
return f"My name is {self.name} and I am {self.age} years old."
# Create an object
person = Person("Ayush", 17)
print(person.introduce()) # Output: My name is Ayush and I am 17 years old.
7. Type Conversion
Python allows you to convert one data type to another. This can be done explicitly using
type conversion functions.
# Examples:
x = 10 # Integer
y = 3.14 # Float