0% found this document useful (0 votes)
2 views6 pages

Python_From_Scratch

This document is a beginner's guide to Python programming, covering fundamental concepts such as variables, data types, user input, basic math, decision making with if statements, loops, functions, and lists. It includes practical exercises and a mini project for creating a number guessing game to reinforce learning. The guide emphasizes Python's readability and ease of use, making it suitable for absolute beginners.

Uploaded by

santosh.gudi1422
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Python_From_Scratch

This document is a beginner's guide to Python programming, covering fundamental concepts such as variables, data types, user input, basic math, decision making with if statements, loops, functions, and lists. It includes practical exercises and a mini project for creating a number guessing game to reinforce learning. The guide emphasizes Python's readability and ease of use, making it suitable for absolute beginners.

Uploaded by

santosh.gudi1422
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python for Absolute Beginners

A from-scratch guide for a complete fresher

What is Python?
Python is a programming language — a way of writing instructions that a computer can follow. It's known for
being readable (almost like English) and beginner-friendly, which is why it's a great first language.

Lesson 1: Your First Program


Every programmer's first program prints a message to the screen.
print("Hello, World!")

print() is a function — a built-in command that does something (here, it displays text). The text inside quotes
"Hello, World!" is called a string.

Lesson 2: Variables
A variable is like a labeled box where you store information.
name = "Alex"
age = 25
print(name)
print(age)

• name = "Alex" → stores the text "Alex" in a box called name


• age = 25 → stores the number 25 in a box called age
• No quotes needed for numbers, quotes needed for text (strings)

Lesson 3: Data Types (the basics)


Type Example / Meaning
int 25 — whole number
float 3.14 — decimal number
str "hello" — text
bool True / False — yes/no value

Exercise: String Concatenation


first_name = "Priya"
last_name = "Sharma"
print("My name is " + first_name + " " + last_name)

Output:
My name is Priya Sharma

+ between strings doesn't add numbers — it joins (concatenates) them together. We added " " (a space) manually
between the names, otherwise it would print "PriyaSharma" with no space.

Lesson 4: Getting Input from the User


Programs get more useful when they can react to what a person types.
name = input("What is your name? ")
print("Nice to meet you, " + name + "!")

• input() pauses the program, shows the message, and waits for the user to type something
• Whatever they type gets stored in the name variable

Lesson 5: Basic Math


a = 10
b = 3

print(a + b) # addition -> 13


print(a - b) # subtraction -> 7
print(a * b) # multiplication -> 30
print(a / b) # division -> 3.333...
print(a // b) # floor division (no decimals) -> 3
print(a % b) # modulus (remainder) -> 1

Exercise: Modulus
x = 7
y = 2
print(x % y)

Output:
1

% (modulus) gives you the remainder after division. 7 divided by 2 is 3 with 1 left over, so 7 % 2 = 1.

Lesson 6: If Statements (Decision Making)


Programs get powerful when they can make decisions. This is where if comes in.
age = 20

if age >= 18:


print("You are an adult")
else:
print("You are a minor")

• if age >= 18: — the colon : at the end is required


• The line below it is indented (4 spaces) — Python uses indentation instead of { } to know what's inside the if block
• else: handles the opposite case
Comparison operators you can use:
Operator Meaning
== equal to
!= not equal to
> greater than
< less than
>= greater or equal
<= less or equal

Multiple conditions with elif


marks = 75

if marks >= 90:


print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")

elif means "else if" — Python checks each condition top to bottom, and runs the first one that's True.

Exercise: elif Chains


temp = 15

if temp > 30:


print("Hot")
elif temp > 20:
print("Warm")
else:
print("Cold")

Output:
Cold

temp = 15 isn't greater than 30, and it isn't greater than 20 either, so both conditions fail and Python falls through
to the else block.

Lesson 7: Loops (Doing Things Repeatedly)


Loops let you repeat an action without writing the same code over and over.

for loop — repeat a fixed number of times


for i in range(5):
print(i)
Output:
0 1 2 3 4

• range(5) generates numbers from 0 to 4 (5 numbers total, starting at 0)


• i is a variable that takes each of those values, one at a time

while loop — repeat while a condition is true


count = 1
while count <= 5:
print(count)
count = count + 1

Output:
1 2 3 4 5

• Keeps running as long as count <= 5 is True


• count = count + 1 increases count each time — without this line, it would loop forever!

A practical example: looping over a list


fruits = ["apple", "banana", "mango"]

for fruit in fruits:


print(fruit)

This prints each fruit one by one. ["apple", "banana", "mango"] is a list — an ordered collection of items.

Exercise: for Loop with Math


for i in range(3):
print(i * 2)

Output:
0 2 4

range(3) gives 0, 1, 2. Each time, we print i * 2: 0*2=0, 1*2=2, 2*2=4.

Lesson 8: Functions (Reusable Blocks of Code)


A function is a named block of code you can reuse instead of retyping it every time.
def greet(name):
print("Hello, " + name + "!")

greet("Priya")
greet("Alex")

Output:
Hello, Priya!
Hello, Alex!

• def = "define" a function


• greet = the function's name
• (name) = a parameter — a placeholder for a value you'll pass in
• greet("Priya") = calling the function, passing "Priya" as the actual value

Functions that return a value


def add(a, b):
return a + b

result = add(5, 3)
print(result)

Output:
8

• return sends a value back out of the function so you can use it later
• Without return, a function just does something (like printing) but doesn't hand back a value you can store

Lesson 9: Lists (In More Depth)


numbers = [10, 20, 30, 40]

print(numbers[0]) # 10 (first item - indexing starts at 0!)


print(numbers[-1]) # 40 (last item)
print(len(numbers)) # 4 (number of items)

[Link](50) # adds 50 to the end


print(numbers) # [10, 20, 30, 40, 50]

Important: indexing starts at 0, not 1. So numbers[0] is the first item, not numbers[1].

Exercise: Functions Returning Values


def square(x):
return x * x

print(square(4))

Output:
16

square(4) returns 16 (4 x 4 = 4 squared).

Mini Project: Number Guessing Game


This project uses variables, loops, if/else, and input, all in one — a great way to see how the building blocks fit
together.
import random

secret_number = [Link](1, 20)


guess = 0
attempts = 0

print("I'm thinking of a number between 1 and 20.")

while guess != secret_number:


guess = int(input("Take a guess: "))
attempts = attempts + 1

if guess < secret_number:


print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print("You got it! The number was " + str(secret_number))
print("It took you " + str(attempts) + " attempts.")

New pieces explained:


• import random — brings in Python's built-in tools for randomness
• [Link](1, 20) — picks a random whole number between 1 and 20 (inclusive)
• int(input(...)) — input() always returns text, so we wrap it in int() to convert it to a number for comparing
• str(secret_number) — converts a number back to text so it can be joined with + in a print statement
• The while guess != secret_number: loop keeps running until the guess is correct

Practice Challenge (Try It Yourself)


Modify the guessing game above: add a rule so that if the player uses more than 5 attempts, the game tells them
"Game over, out of tries!" and stops — even if they haven't guessed correctly.
Hint: you'll need to change the while loop's condition, or add an if check with a break statement inside the loop.

You might also like