1.Dates in Python
Dates in Python
Python has a built-in module called datetime
to work with dates and times. Here’s an overview of how to handle dates in Python.
Creating Date Objects
To create a date, we can use the datetime
module. Here’s how you can create date objects in Python:
import datetime
# Create a date object
x = datetime.datetime(2020, 5, 17)
print(x)
The strftime()
Method
The strftime()
method formats date objects into readable strings. Here are some common date formatting codes:
%Y
: Full year with century (e.g., 2024)%m
: Month as a zero-padded decimal number (01-12)%d
: Day of the month as a zero-padded decimal number (01-31)%H
: Hour (00-23)%M
: Minute (00-59)%S
: Second (00-59)
Example of using strftime()
method:
import datetime
# Get current date and time
now = datetime.datetime.now()
# Format the date and time
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted date and time:", formatted_date)
Additional Examples
Getting the Current Date and Time
import datetime
now = datetime.datetime.now()
print("Current date and time:", now)
Extracting Year, Month, Day, Hour, Minute, and Second
import datetime
now = datetime.datetime.now()
print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)
Parsing Strings into Date Objects
import datetime
date_string = "2024-05-18"
date_object = datetime.datetime.strptime(date_string, "%Y-%m-%d")
print("Date object:", date_object)
Importing the datetime
Module
To use dates in Python, you need to import the datetime
module. This module provides classes for manipulating dates and times.
import datetime
Creating a Date Object
You can create a date object using the datetime
class within the datetime
module. Here’s an example of creating a date object for the current date and time:
import datetime
x = datetime.datetime.now()
print(x)
import datetime
x = datetime.datetime.now()
print(x)
This will output the current date and time.
Accessing Attributes of the Date Object
The datetime
module allows you to access various attributes of the date object, such as year, month, day, hour, minute, and second.
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.month)
print(x.day)
print(x.hour)
print(x.minute)
print(x.second)
Formatting Dates
The strftime()
method allows you to format dates into readable strings. Here are some examples of how to format dates:
import datetime
x = datetime.datetime.now()
# Format the date and time
formatted_date = x.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted date and time:", formatted_date)
Common date formatting codes:
%Y
: Full year with century (e.g., 2024)%m
: Month as a zero-padded decimal number (01-12)%d
: Day of the month as a zero-padded decimal number (01-31)%H
: Hour (00-23)%M
: Minute (00-59)%S
: Second (00-59)
Example Code
Here is a full example showing how to import the datetime
module, create a date object, and access its attributes:
import datetime
# Create a date object
x = datetime.datetime.now()
# Print the date object
print(x)
# Access and print individual attributes
print("Year:", x.year)
print("Month:", x.month)
print("Day:", x.day)
print("Hour:", x.hour)
print("Minute:", x.minute)
print("Second:", x.second)
# Format the date and print it
formatted_date = x.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted date and time:", formatted_date)
2.Math in Python
Math in Python
Python provides various built-in math functions and a dedicated module named math
to perform mathematical operations. Here’s an overview of math in Python.
Built-in Math Functions
Python offers several built-in functions that are useful for mathematical operations:
abs()
: Returns the absolute value of a number.max()
: Returns the maximum of a set of numbers.min()
: Returns the minimum of a set of numbers.pow()
: Returns the value of x to the power of y.round()
: Rounds a number to a specified number of decimal places.
Example:
print(abs(-7)) # Output: 7
print(max(3, 5, 1)) # Output: 5
print(min(3, 5, 1)) # Output: 1
print(pow(2, 3)) # Output: 8
print(round(5.567, 2)) # Output: 5.57
The math
Module
The math
module provides more advanced mathematical functions and constants. To use this module, you need to import it first:
import math
Some of the key functions and constants in the math
module include:
math.sqrt(x)
: Returns the square root of x.math.factorial(x)
: Returns the factorial of x.math.sin(x)
: Returns the sine of x (x is in radians).math.cos(x)
: Returns the cosine of x (x is in radians).math.pi
: The mathematical constant π (pi).math.e
: The mathematical constant e (base of natural logarithm).
Example:
import math
print(math.sqrt(16)) # Output: 4.0
print(math.factorial(5)) # Output: 120
print(math.sin(math.pi / 2)) # Output: 1.0
print(math.cos(math.pi)) # Output: -1.0
print(math.pi) # Output: 3.141592653589793
print(math.e) # Output: 2.718281828459045
Example Code
Here is a complete example showing the usage of built-in math functions and the math
module:
import math
# Built-in functions
absolute_value = abs(-10)
maximum_value = max(10, 20, 30)
minimum_value = min(10, 20, 30)
power_value = pow(2, 3)
rounded_value = round(5.678, 2)
print("Absolute Value:", absolute_value)
print("Maximum Value:", maximum_value)
print("Minimum Value:", minimum_value)
print("Power Value:", power_value)
print("Rounded Value:", rounded_value)
# Math module functions
square_root = math.sqrt(25)
factorial_value = math.factorial(5)
sine_value = math.sin(math.pi / 2)
cosine_value = math.cos(math.pi)
pi_value = math.pi
e_value = math.e
print("Square Root:", square_root)
print("Factorial:", factorial_value)
print("Sine Value:", sine_value)
print("Cosine Value:", cosine_value)
print("Pi Value:", pi_value)
print("e Value:", e_value)