Python for Absolute Beginners: Full Guide with Code Examples
Python Basics for Absolute Beginners: Complete Guide
Welcome to TECH WORLD! If you're new to Python, you're in the right place. This tutorial covers all the basics you need to start writing your own programs—from printing messages to writing simple logic. Let's dive in!
1. Printing Output
Use the print()
function to display messages to the user.
print("Welcome to TECH WORLD!")
Explanation:
print()
is a built-in function that displays text.- Text is wrapped in double or single quotes.
2. Variables and Data Types
Variables store data that you can use later. Python handles different data types like text, numbers, and more.
name = "Alice" # String
age = 30 # Integer
height = 5.7 # Float
is_coder = True # Boolean
Explanation:
name
is a string (text).age
is an integer (whole number).height
is a float (decimal number).is_coder
is a boolean (True/False).
3. User Input
Get data from the user using the input()
function.
username = input("Enter your name: ")
print("Hello, " + username + "!")
Note: input()
always returns a string.
4. Type Conversion
Convert strings to numbers using int()
or float()
.
age = input("Enter your age: ")
age = int(age)
print("Next year, you will be", age + 1)
Why? You can't do math with strings, so we convert them to integers.
5. Conditional Statements (if/else)
Use conditions to make decisions in your program.
age = int(input("Enter your age: "))
if age >= 18:
print("You're an adult.")
else:
print("You're underage.")
Explanation:
if
checks a condition.else
runs if the condition is false.:
is required after each condition line.
6. Loops (for and while)
For Loop: Repeats a block of code a set number of times.
for i in range(5):
print("Loop number:", i)
While Loop: Repeats while a condition is true.
count = 0
while count < 5:
print("Count is", count)
count += 1
7. Functions
Functions group code together to reuse later.
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
greet("Bob")
Explanation: Use def
to define a function. Call it by name.
8. Final Mini Project: Greeting Bot
Let’s combine what we’ve learned into a small program!
def greet_user():
name = input("What is your name? ")
age = int(input("How old are you? "))
print("Hello,", name + "!")
if age < 18:
print("You’re still young and learning!")
else:
print("Keep building cool stuff!")
greet_user()
This project uses input, type conversion, conditionals, and functions together.
What’s Next?
- Learn about lists and dictionaries
- Build basic games with conditions and loops
- Understand error handling and debugging
Stay tuned on TECH WORLD for the next tutorial!
Comments
Post a Comment