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:
- Integers (
int): Whole numbers, e.g.,10,-5. - Floating-point numbers (
float): Numbers with a decimal point, e.g.,3.14,-2.5. - Strings (
str): Sequences of characters, enclosed in single or double quotes, e.g.,"Hello",'Python'. - Booleans (
bool): Represent truth values, eitherTrueorFalse. - Lists (
list): Ordered, mutable collections of items, e.g.,[1, "apple", 3.14]. - Tuples (
tuple): Ordered, immutable collections of items, e.g.,(1, "banana", 2.71). - Dictionaries (
dict): Unordered collections of key-value pairs, e.g.,{"name": "Alice", "age": 30}.
Example:
name = "Alice"
age = 30
height = 5.5
is_student = True
print(f"Name: {name}, Age: {age}, Height: {height}, Student: {is_student}")
Operators
Operators are special symbols that perform operations on variables and values.
Types of Operators:
- Arithmetic Operators:
+,-,*,/,%(modulo),**(exponentiation),//(floor division). - Comparison Operators:
==(equal to),!=(not equal to),>(greater than),<(less than),>=(greater than or equal to),<=(less than or equal to). - Logical Operators:
and,or,not. - Assignment Operators:
=,+=,-=, etc.
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}")
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:
- if: Executes a block of code if a condition is true.
- elif: Executes a block of code if the previous condition was false and this condition is true.
- else: Executes a block of code if all previous conditions were false.
Loops:
- for loop: Iterates over a sequence (like a list or string).
- while loop: Executes a block of code as long as a condition is true.
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.")
Example (for loop):
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
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)
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}")
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:
- Object-Oriented Programming (OOP) in Python
- File Handling
- Error Handling (Exceptions)
- Working with Libraries (e.g., NumPy, Pandas, Requests)
- Web Development Frameworks (e.g., Flask, Django)
Keep practicing, building projects, and exploring the vast Python ecosystem!