PyGuide

Learn Python with practical tutorials and code examples

Python Basics: Getting Started with Python Programming

Python is one of the most popular programming languages in the world, known for its simplicity and readability. In this tutorial, we'll cover the fundamental concepts you need to know to start programming in Python.

What is Python? #

Python is a high-level, interpreted programming language that emphasizes code readability and simplicity. It was created by Guido van Rossum and first released in 1991.

Key Features of Python: #

  • Easy to learn: Simple syntax similar to English
  • Versatile: Used for web development, data science, automation, and more
  • Large community: Extensive libraries and frameworks available
  • Cross-platform: Runs on Windows, macOS, and Linux

Variables and Data Types #

Variables #

Variables are containers for storing data values. In Python, you don't need to declare the type of variable explicitly.

# Creating variables
name = "Alice"
age = 25
height = 5.6
is_student = True

print(name)    # Output: Alice
print(age)     # Output: 25
print(height)  # Output: 5.6
print(is_student)  # Output: True

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Data Types #

Python has several built-in data types:

1. Numbers #

# Integer
count = 42
print(type(count))  # Output: <class 'int'>

# Float
price = 19.99
print(type(price))  # Output: <class 'float'>

# Complex
complex_num = 3 + 4j
print(type(complex_num))  # Output: <class 'complex'>

2. Strings #

# String creation
message = "Hello, World!"
multiline = """This is a
multiline string"""

# String operations
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)  # Output: John Doe

# String methods
print(message.upper())  # Output: HELLO, WORLD!
print(message.lower())  # Output: hello, world!
print(len(message))     # Output: 13

3. Booleans #

is_active = True
is_finished = False

# Boolean operations
print(is_active and is_finished)  # Output: False
print(is_active or is_finished)   # Output: True
print(not is_active)              # Output: False

Basic Operators #

Arithmetic Operators #

a = 10
b = 3

print(a + b)  # Addition: 13
print(a - b)  # Subtraction: 7
print(a * b)  # Multiplication: 30
print(a / b)  # Division: 3.3333...
print(a // b) # Floor division: 3
print(a % b)  # Modulus: 1
print(a ** b) # Exponentiation: 1000

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Comparison Operators #

x = 5
y = 10

print(x == y)  # Equal: False
print(x != y)  # Not equal: True
print(x < y)   # Less than: True
print(x > y)   # Greater than: False
print(x <= y)  # Less than or equal: True
print(x >= y)  # Greater than or equal: False

Input and Output #

Getting User Input #

# Getting input from user
name = input("What's your name? ")
age = int(input("How old are you? "))

print(f"Hello {name}, you are {age} years old!")

Printing Output #

# Different ways to print
print("Hello, World!")
print("Name:", name)
print(f"Age: {age}")  # f-string formatting
print("Sum:", 10 + 5)

Control Flow - Conditional Statements #

If-Else Statements #

age = 18

if age >= 18:
    print("You are an adult")
elif age >= 13:
    print("You are a teenager")
else:
    print("You are a child")

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Multiple Conditions #

temperature = 25
is_sunny = True

if temperature > 20 and is_sunny:
    print("Perfect weather for a picnic!")
elif temperature > 20:
    print("Good temperature, but might be cloudy")
else:
    print("Too cold for outdoor activities")

Common Mistakes to Avoid #

  1. Indentation Errors: Python uses indentation to define code blocks
# Wrong
if True:
print("This will cause an error")

# Correct
if True:
    print("This is properly indented")
  1. Case Sensitivity: Python is case-sensitive
Name = "Alice"
name = "Bob"
print(Name)  # Output: Alice
print(name)  # Output: Bob
  1. Variable Naming: Use descriptive names and follow conventions
# Good
student_name = "John"
total_score = 95

# Avoid
x = "John"
ts = 95

Practice Exercise #

Try creating a simple program that:

  1. Asks for the user's name and age
  2. Calculates the year they were born
  3. Prints a personalized message
# Solution
name = input("What's your name? ")
age = int(input("How old are you? "))
current_year = 2024
birth_year = current_year - age

print(f"Hello {name}! You were born in {birth_year}.")

Next Steps #

Now that you've learned the basics of Python, you're ready to explore more advanced topics:

  • Data structures: Lists, dictionaries, tuples, and sets
  • Functions: Creating reusable code blocks
  • Loops: Repeating code efficiently
  • File handling: Reading and writing files
  • Error handling: Managing exceptions

Keep practicing with small projects and gradually build more complex programs. Remember, the key to learning programming is consistent practice!

Summary #

In this tutorial, we covered:

  • What Python is and why it's popular
  • Variables and data types (numbers, strings, booleans)
  • Basic operators (arithmetic, comparison)
  • Input and output operations
  • Conditional statements (if-else)
  • Common mistakes to avoid
  • A practical exercise

Continue your Python journey by exploring our other tutorials and building small projects to reinforce your learning!