Python
Python
Python
2. Hello, World!
Let's start by writing the classic "Hello, World!" program to make sure everything
is working properly.
Code:
print("Hello, World!")
Python is dynamically typed, so you don't need to declare variable types. You can
directly assign values to variables.
Example:
age = 25 # Integer
height = 5.9 # Float
name = "Alice" # String
is_student = True # Boolean
Output:
Conditional Statements (if/else): Python uses if, elif (else if), and else for
conditional logic.
age = 18
Loops:
for i in range(5):
print(i)
i = 0
while i < 5:
print(i)
i += 1
5. Functions
Functions in Python allow you to reuse code and perform tasks in an organized
manner.
Example:
def greet(name):
print(f"Hello, {name}!")
6. Lists (Arrays)
Lists are one of Python's built-in data structures that hold an ordered collection
of items. You can store different types of data in a single list.
Example:
fruits.append("orange")
fruits.remove("banana")
fruits[0] = "grape"
7. Dictionaries
Example:
student = {
"name": "Alice",
"age": 22,
"major": "Computer Science"
}
You can access values by their keys, add new key-value pairs, or modify
existing ones.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
Python uses try and except blocks to handle errors gracefully without crashing the
program.
Example:
try:
x = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
Python has a large standard library, and you can also install third-party libraries
using pip (Python's package manager). For example, you can install libraries for
web development, data analysis, machine learning, etc.
Example:
response = requests.get("https://example.com")
print(response.text)
Now that you’ve learned some basics, here are some topics to dive deeper into:
Would you like to focus on a specific area, or have any particular questions about
Python? Let me know how you want to proceed!