Beginner

Python Basics

Get started with Python programming fundamentals

Introduction

Welcome to Python programming! Python is one of the most popular and versatile programming languages in the world. It's used for web development, data science, artificial intelligence, automation, and much more. In this tutorial, you'll learn the fundamental building blocks of Python programming.

What You'll Learn

💡 Tip: Python is known for its simple, readable syntax. It's perfect for beginners and powerful enough for experts!

Your First Python Program

Let's start with the classic "Hello, World!" program. This simple program displays text on the screen.

# This is your first Python program
print("Hello, World!")

To run this program:

  1. Open your text editor or Python IDE
  2. Type the code above
  3. Save the file as hello.py
  4. Run it using python hello.py in your terminal

Variables and Data Types

Variables are containers for storing data values. In Python, you don't need to declare variable types explicitly - Python figures it out automatically!

Creating Variables

# String variable
name = "Alice"

# Integer variable
age = 25

# Float variable
height = 5.6

# Boolean variable
is_student = True

# Print variables
print(name)
print(age)
print(height)
print(is_student)

Common Data Types

💡 Tip: You can check a variable's type using the type() function: type(name)

Basic Operators

Python supports various operators for performing calculations and comparisons.

Arithmetic Operators

# Addition
result = 10 + 5      # 15

# Subtraction
result = 10 - 5      # 5

# Multiplication
result = 10 * 5      # 50

# Division
result = 10 / 5      # 2.0

# Floor Division
result = 10 // 3     # 3

# Modulus (remainder)
result = 10 % 3      # 1

# Exponentiation
result = 2 ** 3      # 8

Comparison Operators

x = 10
y = 5

print(x > y)    # True (greater than)
print(x < y)    # False (less than)
print(x == y)   # False (equal to)
print(x != y)   # True (not equal to)
print(x >= y)   # True (greater than or equal)
print(x <= y)   # False (less than or equal)

User Input and Output

Python makes it easy to get input from users and display output.

Getting User Input

# Ask user for their name
name = input("What is your name? ")
print("Hello, " + name + "!")

# Ask for age (convert to integer)
age = int(input("How old are you? "))
print("You are " + str(age) + " years old.")

⚠️ Note: The input() function always returns a string. Use int() or float() to convert to numbers.

String Formatting

Python offers multiple ways to format strings:

name = "Alice"
age = 25

# Method 1: Concatenation
print("My name is " + name + " and I am " + str(age))

# Method 2: f-strings (recommended)
print(f"My name is {name} and I am {age}")

# Method 3: format() method
print("My name is {} and I am {}".format(name, age))

Comments

Comments are notes in your code that Python ignores. They help you and others understand your code.

# This is a single-line comment

"""
This is a multi-line comment.
It can span multiple lines.
Use it for longer explanations.
"""

x = 5  # You can also add comments after code

Practice Exercise

Now it's your turn! Try creating a simple program that:

  1. Asks the user for their name
  2. Asks for their favorite number
  3. Calculates their number multiplied by 2
  4. Prints a message with all this information

💡 Challenge: Try to use f-strings for formatting your output!

Example Solution

# Get user input
name = input("What is your name? ")
fav_number = int(input("What is your favorite number? "))

# Calculate
doubled = fav_number * 2

# Display result
print(f"Hello {name}! Your favorite number is {fav_number}.")
print(f"When doubled, it becomes {doubled}!")

Summary

Congratulations! You've learned the basics of Python programming. Here's what we covered:

What's Next?

Now that you understand Python basics, you're ready to learn about:

💡 Keep Practicing: The best way to learn programming is by writing code. Try modifying the examples and creating your own programs!

🚀 Ready to Build Something?

Now that you understand Python basics, let's create your first real project! This hands-on exercise will help you practice everything you've learned.

📝 Knowledge Check

Test your understanding of Python basics!

Question 1: Which function is used to display output in Python?

A) display()
B) print()
C) output()
D) show()

Question 2: What data type is the value True?

A) String
B) Integer
C) Boolean
D) Float

Question 3: How do you get user input in Python?

A) input()
B) get()
C) read()
D) scan()

Question 4: What symbol starts a comment in Python?

A) //
B) /*
C) --
D) #

Question 5: What does the ** operator do?

A) Multiplication
B) Exponentiation (power)
C) Division
D) Addition
← Previous: Variables Back to Python Course →