Python 3 beginner course · Chapter 5 of 10
This chapter explains Dates, Functions, Built-ins and Modules in plain language. Read the concept, run the example, check the result and change one part to learn by doing. Optional packages are identified in their sections.
Date and time with datetime
date represents a calendar day, time a clock time, and datetime both. Use an aware datetime with a timezone for real timestamps. Format display text separately from the stored value.
Python example
from datetime import date
birthday = date(2020, 5, 14)
print(birthday.year, birthday.month)Expected result: 2020 5
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Calendar module
The calendar module can show month layouts and answer weekday questions. Weekday numbers start at Monday = 0. Use it when scheduling by calendar date rather than manually counting days.
Python example
import calendar
print(calendar.weekday(2024, 1, 1))Expected result: 0, because 1 January 2024 was Monday.
Practice: Predict the result before running the example. Change one value or line and explain the new result.
User-defined functions
def names a reusable block. Parameters receive input and return sends a value back. Keep a function focused on one job and give it a descriptive name. A function with no return statement returns None.
Python example
def total(price, quantity):
return price * quantity
print(total(12, 3))Expected result: 36
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Built-in functions
Python includes built-ins such as len(), sum(), min(), max() and sorted(). Reuse them instead of writing a loop for every common task. Check the expected input type and return value.
Python example
scores = [80, 65, 90]
print(len(scores), sum(scores), max(scores))Expected result: 3 235 90
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Modules and imports
A module is a .py file of related code. import gives access to its names. The standard library provides many ready-made modules; third-party packages are separate installations. Avoid naming your own file after a standard module.
Python example
import math
print(math.isqrt(81))Expected result: 9
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Function examples: default and keyword arguments
Default arguments provide an optional value. Keyword arguments make a call easy to read. Avoid using a mutable list or dictionary as a default because it would be shared across calls.
Python example
def greet(name, greeting="Hello"):
return f"{greeting}, {name}"
print(greet("Asha", greeting="Welcome"))Expected result: Welcome, Asha
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Quick review
Explain the main idea to a friend in one sentence. Then recreate one example without looking at the code. If it fails, read the error message and compare the indentation and names.
Continue through the complete Python learning path. These are original explanations and diagrams; the topic checklist was compared with the w3resource Python index and the official Python tutorial.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.