Basics of Python

Fri Feb 21, 2025

Python Basics: A Beginner's Guide

Python Basics: A Beginner's Guide

Python is a powerful, easy-to-learn programming language used in various fields. This guide covers fundamental concepts to help beginners get started.

Variables and Data Types

Python supports different data types such as strings, integers, floats, and booleans.

name = "Alice"
age = 25
height = 5.4
is_student = True
print(name, age, height, is_student)

User Input

You can take input from users using the input() function.

name = input("Enter your name: ")
print("Hello, " + name + "!")

Conditional Statements

Use if-else to execute code based on conditions.

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("Even Number")
else:
    print("Odd Number")

Loops in Python

Loops allow you to iterate over a sequence of values.

for i in range(1, 6):
    print("Hello", i)

Functions in Python

Functions allow code reuse and modularity.

def greet(name):
    print("Hello, " + name + "!")
greet("Alice")

Lists in Python

Lists store multiple items in a single variable.

fruits = ["Apple", "Banana", "Cherry"]
print(fruits[0])

Dictionaries in Python

Dictionaries store key-value pairs.

person = {"name": "Alice", "age": 25}
print(person["name"])

Object-Oriented Programming

Python supports OOP with classes and objects.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def greet(self):
        print("Hello, my name is " + self.name)

p1 = Person("Alice", 25)
p1.greet()