Python Dates
Python Dates
A date in Python is not a data type of its own, but we can import a module
named datetime to work with dates as date objects.
import datetime
x = [Link]()
print(x)
Date Output
The date contains year, month, day, hour, minute, second, and microsecond.
The datetime module has many methods to return information about the date
object.
import datetime
x = [Link]()
print([Link])
print([Link]("%A"))
The strftime() Method
The datetime object has a method for formatting date objects into readable
strings.
The method is called strftime(), and takes one parameter, format, to specify
the format of the returned string.
import datetime
x = [Link](2018, 6, 1)
print([Link]("%B"))
Creating Date Objects
To create a date, we can use the datetime() class (constructor) of the
datetime module.
The datetime() class requires three parameters to create a date: year, month,
day.
import datetime
x = [Link](2020, 5, 17)
print(x)
A reference of all the legal format codes
Directive Description Example
%a Weekday, short version Wed
%A Weekday, full version Wednesday
%w Weekday as a number 0-6, 0 is Sunday 3
%d Day of month 01-31 31
%b Month name, short version Dec
%B Month name, full version December
%m Month as a number 01-12 12
%y Year, short version, without century 18
%Y Year, full version 2018
A reference of all the legal format codes
Directive Description Example
%H Hour 00-23 17
%I Hour 00-12 05
%p AM/PM PM
%M Minute 00-59 41
%S Second 00-59 08
%f Microsecond 000000-999999 548513
%z UTC offset +0100
%Z Timezone CST
%j Day number of year 001-366 365
A reference of all the legal format codes
Directive Description Example
Week number of year, Sunday as
%U 52
the first day of week, 00-53
Week number of year, Monday as
%W 52
the first day of week, 00-53
%c Local version of date and time Mon Dec 31 [Link] 2018
%C Century 20
%x Local version of date 12/31/18
%X Local version of time [Link]
%% A % character %
%G ISO 8601 year 2018
A reference of all the legal format codes
Directive Description Example
%u ISO 8601 weekday (1-7) 1
%V ISO 8601 weeknumber (01-53) 01
Python Math
Python Math
Python has a set of built-in math functions, including an extensive math
module, that allows you to perform mathematical tasks on numbers.
The min() and max() functions can be used to find the lowest or highest value
in an iterable:
x = min(5, 10, 25)
y = max(5, 10, 25)
print(x)
print(y)
The abs() function returns the absolute (positive) value of the specified
number:
x = abs(-7.25)
print(x)
The pow(x, y) function returns the value of x to the power of y (xy).
x = pow(4, 3)
print(x)
The Math Module
Python has also a built-in module called math, which extends the list of
mathematical functions.
To use it, you must import the math module:
import math
When you have imported the math module, you can start using methods and
constants of the module.
The [Link]() method for example, returns the square root of a number.
import math
x = [Link](64)
print(x)
The [Link]() method rounds a number upwards to its nearest integer, and
the [Link]() method rounds a number downwards to its nearest integer,
and returns the result.
import math
x = [Link](1.4)
y = [Link](1.4)
print(x) # returns 2
print(y) # returns 1
The [Link] constant, returns the value of PI (3.14...).
import math
x = [Link]
print(x)
Python PIP
What is PIP?
PIP is a package manager for Python packages, or modules if you like.
What is a Package?
A package contains all the files you need for a module.
Modules are Python code libraries you can include in your project.
Check if PIP is Installed
Navigate your command line to the location of Python's script directory, and
type the following.
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-
32\Scripts>pip --version
Install PIP
If you do not have PIP installed, you can download and install it from this
page: [Link]
Download a Package
Downloading a package is very easy.
Open the command line interface and tell PIP to download the package you
want.
Navigate your command line to the location of Python's script directory, and
type the following:
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-
32\Scripts>pip install camelcase
Using a Package
Once the package is installed, it is ready to use.
Import the "camelcase" package into your project.
import camelcase
c = [Link]()
txt = "hello world"
print([Link](txt))
Remove a Package
Use the uninstall command to remove a package:
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-
32\Scripts>pip uninstall camelcase
The PIP Package Manager will ask you to confirm that you want to remove the
camelcase package.
Press y and the package will be removed.
List Packages
Use the list command to list all the packages installed on your system:
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-
32\Scripts>pip list
Result:
Package Version
-----------------------
camelcase 0.2
mysql-connector 2.1.6
pip 18.1
pymongo 3.6.1
setuptools 39.0.1
And Congratulations! You’re now
a python junior programmar.