DEV Community

tamilvanan
tamilvanan

Posted on

Learn Python 001

Note: I’m not an expert. I’m writing this blog just to document my learning journey. πŸš€


1. What is Python?

Python is a tool we use to give instructions to a computer.
It’s a programming language β€” a way to write tasks in simple, readable English-like words so the computer can follow them.


2. What is a Program?

A program is a list of instructions that a computer runs step by step.

To write a program in Python, we write instructions using Python’s rules (called syntax). The instructions tell the computer what to do with data (numbers, text, etc.).


3. What is Data?

Data is information β€” like:

  • Numbers (10)
  • Text ("hello")
  • True/False values (True, False)

Every piece of data has a type. Common types in Python:

  • int: whole numbers like 5
  • float: decimal numbers like 3.14
  • str: text like "Python"
  • bool: truth values: True or False

4. What are Variables?

A variable is a name you give to a piece of data so you can use it later.

age = 25
name = "Alice"
Enter fullscreen mode Exit fullscreen mode

Here, age stores the number 25, and name stores the word "Alice".


5. What Can You Do With Data?

You use operators to do things with data.

Math:

5 + 2   # 7
5 - 2   # 3
5 * 2   # 10
5 / 2   # 2.5
Enter fullscreen mode Exit fullscreen mode

Comparisons:

5 > 2      # True
3 == 3     # True
4 != 5     # True
Enter fullscreen mode Exit fullscreen mode

6. How to Make Decisions in Code

We use if statements to tell Python: β€œOnly do this if a condition is true.”

age = 18

if age >= 18:
    print("You are an adult")
else:
    print("You are a minor")
Enter fullscreen mode Exit fullscreen mode

Python checks the condition. If it's true, it runs the code under if. Otherwise, it runs the code under else.


7. How to Repeat Things

You use loops to repeat actions.

for loop – repeats a set number of times:

for i in range(3):
    print("Hello")
Enter fullscreen mode Exit fullscreen mode

This prints "Hello" three times.

while loop – repeats while a condition is true:

count = 0
while count < 3:
    print(count)
    count += 1
Enter fullscreen mode Exit fullscreen mode

This prints 0, 1, and 2.


8. Functions: Reusable Code

A function is a block of code that does one job. You can run it (call it) whenever you want.

def greet(name):
    print("Hello", name)

greet("Alice")
Enter fullscreen mode Exit fullscreen mode

You define it with def, give it a name, and call it by writing its name and giving it any needed inputs.


9. Lists: A Collection of Values

A list holds multiple values in one variable.

fruits = ["apple", "banana", "cherry"]
Enter fullscreen mode Exit fullscreen mode

You can get items by position:

print(fruits[0])  # apple
Enter fullscreen mode Exit fullscreen mode

You can also add to it:

fruits.append("orange")
Enter fullscreen mode Exit fullscreen mode

10. Dictionaries: Key-Value Pairs

A dictionary stores values with names (keys), like a mini-database.

person = {"name": "Alice", "age": 30}
print(person["name"])  # Alice
Enter fullscreen mode Exit fullscreen mode

11. Handling Errors

Sometimes code breaks. You can handle errors using try and except.

try:
    print(10 / 0)
except ZeroDivisionError:
    print("Can't divide by zero")
Enter fullscreen mode Exit fullscreen mode

This stops the program from crashing.


12. Using Tools: Importing Modules

Python has extra features stored in modules. You can bring them into your code using import.

import math
print(math.sqrt(16))  # 4.0
Enter fullscreen mode Exit fullscreen mode

13. Classes and Objects: Building Custom Types

A class is like a blueprint. It defines what something is and what it can do.
An object is a thing made from that blueprint.

class Dog:
    def __init__(self, name):
        self.name = name

    def bark(self):
        print(self.name + " says woof!")

my_dog = Dog("Buddy")
my_dog.bark()  # Buddy says woof!
Enter fullscreen mode Exit fullscreen mode

You use classes to organize your code and model real things.


Summary

Python lets you:

  • Store data in variables
  • Use operators to work with data
  • Make decisions with if
  • Repeat tasks with loops
  • Reuse code with functions
  • Group data with lists and dictionaries
  • Handle errors
  • Use built-in tools (modules)
  • Build custom structures with classes

Top comments (0)