DEV Community

tamilvanan
tamilvanan

Posted on

Learn Python 002

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


๐Ÿง  What is Python?

Python is a programming language. But what does that really mean?

โœ… First Principles:

A programming language is a system of precise instructions we give to computers so they can solve problems automatically.

Python was designed to make this system of instructions:

  • Simple to read
  • Easy to write
  • Powerful enough to solve real problems

๐Ÿชœ Step-by-Step Python Basics (Ground-Up)


1. ๐Ÿ–จ๏ธ print() โ€” The First Tool

โœ… What is it?

print() tells the computer to display something โ€” itโ€™s your first way to talk back to the user (or yourself).

print("Hello, world!")
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Why it matters:

  • Programming is invisible โ€” the computer wonโ€™t tell you what itโ€™s thinking.
  • print() helps you see whatโ€™s happening in your code.
  • It's like debugging with a flashlight.

2. ๐Ÿงบ Variables โ€” Naming Your Data

โœ… What is a variable?

A variable is a name for a piece of data.

name = "Alice"
age = 25
Enter fullscreen mode Exit fullscreen mode
  • name is a label
  • "Alice" is a string
  • age is a label for the number 25

๐Ÿง  Why it matters:

You canโ€™t solve problems if you canโ€™t remember things.
Variables are how a program stores memory.


3. ๐Ÿง  Data Types โ€” What Kinds of Data Can We Use?

Type Example What it Represents
int 10 Whole numbers
float 3.14 Decimal numbers
str "Hello" Text (string of characters)
bool True/False Logical values
list [1, 2, 3] Sequence of values
dict {"a": 1} Key-value pairs

๐Ÿง  Why it matters:

Each data type tells Python what kind of operations it can do:

  • int and float can be added/subtracted
  • str can be joined ("Hello" + " World")
  • list and dict can be looped over, indexed, and changed

4. ๐Ÿ” Control Flow โ€” Making Decisions

โœ… Problem: How do we tell Python to choose?

age = 17

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

๐Ÿง  Why it matters:

Programming is not linear.
You need to say โ€œDo this only if that is true.โ€

Control flow gives you:

  • if: run if condition is true
  • else: run if not
  • elif: try another condition if the first fails

5. ๐Ÿ”‚ Loops โ€” Doing Something Repeatedly

โœ… Problem: I want to do the same task many times

for i in range(5):
    print(i)
Enter fullscreen mode Exit fullscreen mode
  • range(5) means: 0, 1, 2, 3, 4
  • i is the variable that changes each time
  • print(i) happens once per loop

๐Ÿง  Why it matters:

Computers are good at repetition.
Donโ€™t repeat yourself โ€” loop instead.


6. ๐Ÿงช Input โ€” Talk to the User

name = input("What is your name? ")
print("Hello,", name)
Enter fullscreen mode Exit fullscreen mode
  • input() pauses and waits for the user to type
  • The result is always a str

๐Ÿง  Why it matters:

Good programs are interactive.
Input lets your code respond to human action.


7. ๐Ÿงฎ Real-World Program Example

Convert Celsius to Fahrenheit

c = float(input("Enter temperature in Celsius: "))
f = (c * 9 / 5) + 32
print("Fahrenheit:", f)
Enter fullscreen mode Exit fullscreen mode

This uses:

  • Input
  • Type conversion (float)
  • Math
  • Output

๐Ÿง  Why it matters:

Programming turns formulas into tools.


8. ๐Ÿงฑ Functions โ€” Reusable Logic

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

greet("Alice")
greet("Bob")
Enter fullscreen mode Exit fullscreen mode
  • def creates a new function
  • greet() calls it

๐Ÿง  Why it matters:

Repetition is wasteful.
If a task happens often, turn it into a function.


โœ… Summary: What You Now Know

Concept What It Solves
print() See what your program is doing
Variables Store data with a name
Data Types Work with different kinds of values
if/else Make decisions
for loops Repeat actions
input() Get user input
Functions Reuse logic

Top comments (0)