Python Basics for Beginners
Python Basics for Beginners
1. Introduction to Python
Python is a popular programming language known for its simplicity and readability. It is easy to learn
and has extensive libraries.
You can install Python from the official website: https://www.python.org/downloads/
2. Basic Syntax
Example 1: Print Statement
Code:
print("Hello, World!")
Explanation: Prints 'Hello, World!' to the console.
3. Data Types
Example: Strings
Code:
name = "Alice"
print(name.upper())
Explanation: Converts the string 'Alice' to uppercase.
4. Conditional Statements
Example:
num = 10
if num > 0:
Python Basics for Beginners
print("Positive number")
Explanation: Checks the value of num and prints an appropriate message.
5. Loops
Example: For Loop
Code:
for i in range(5):
print(i)
Explanation: Loops from 0 to 4, printing each value.
6. Functions
Example:
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Explanation: Defines a function that returns a greeting message.
7. Basic File Handling
Example:
# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, File!")
# Reading from a file
with open("example.txt", "r") as file:
Python Basics for Beginners
content = file.read()
print(content)
Explanation: Demonstrates how to write to and read from a file.
8. Simple Examples
Example 1: Adding Two Numbers
Code:
num1 = 5
num2 = 10
print(num1 + num2)
Explanation: Adds two numbers and prints the result.
Example 2: Finding the Largest Number
Code:
a, b, c = 5, 10, 3
print(max(a, b, c))
Explanation: Finds and prints the largest of three numbers.