Python Basics: A Foundation for Development

Introduction to Python

Welcome to the foundational course on Python programming. Python is a versatile, high-level, interpreted programming language known for its readability and ease of use. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.

Microsoft is committed to empowering developers with the best tools and learning resources. This module will guide you through the essential concepts of Python, preparing you for more advanced topics and real-world application development.

Variables and Data Types

Variables are used to store data values. In Python, you don't need to declare the type of a variable explicitly; the interpreter infers it.

Common Data Types:

Example:


name = "Alice"
age = 30
height = 5.5
is_student = True

print(f"Name: {name}, Age: {age}, Height: {height}, Student: {is_student}")
            
Name: Alice, Age: 30, Height: 5.5, Student: True

Operators

Operators are special symbols that perform operations on variables and values.

Types of Operators:

Example:


x = 10
y = 5

print(f"Addition: {x + y}")
print(f"Is x greater than y? {x > y}")
print(f"Is x positive and y positive? {x > 0 and y > 0}")
            
Addition: 15
Is x greater than y? True
Is x positive and y positive? True

Control Flow

Control flow statements allow you to execute different blocks of code based on certain conditions or to repeat code.

Conditional Statements:

Loops:

Example (if-elif-else):


temperature = 25

if temperature > 30:
    print("It's hot!")
elif temperature > 20:
    print("It's warm.")
else:
    print("It's cool.")
            
It's warm.

Example (for loop):


fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
            
apple
banana
cherry

Functions

Functions are blocks of reusable code that perform a specific task. They help in organizing code and making it more modular.

Functions are defined using the def keyword.

Example:


def greet(name):
    """This function greets the person passed in as a parameter."""
    return f"Hello, {name}!"

message = greet("Bob")
print(message)
            
Hello, Bob!

Modules and Packages

Modules are Python files containing Python definitions and statements. Packages are collections of modules.

You can use the import statement to access functionality from modules.

Example (using the math module):


import math

radius = 5
area = math.pi * radius**2
print(f"The area of a circle with radius {radius} is: {area:.2f}")
            
The area of a circle with radius 5 is: 78.54

Next Steps

Congratulations on completing the Python Basics module! You've learned about variables, data types, operators, control flow, functions, and modules.

To continue your journey, explore these topics:

Keep practicing, building projects, and exploring the vast Python ecosystem!