Riyad Amin

Riyad Amin

1571193821

Python Datetime Module For Beginners

“The python DateTime module supplies classes for manipulating dates and times in both simple and complex ways.”

So, the Python DateTime modules contain several classes. Let us discuss them one by one.

Python DateTime Modules : datetime.datetime

Let’s discuss how to use the DateTime.datetime class.

Datetime.datetime.today()

datetime.datetime.today() prints today’s date. See the example below.

>>> print datetime.datetime.today()
2018–08–19 22:49:24.169000

datetime.datetime.now()

datetime.datetime.now() displays the same output as that produced by the datetime.datetime.today().

>>> print datetime.datetime.now()
2018–08–19 22:49:51.541000

But if you provide the time zone then datetime.datetime.now() returns the current time of the specified time zone.

>>>
>>> import pytz
>>> pytz.utc
>>> print datetime.datetime.now(pytz.utc)
2018–08–19 17:23:34.614000+00:00

If you provide the time zone information in a string then the interpreter throws an error.

>>> print datetime.datetime.now(‘US/Eastern’)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type ‘str’
>>>

datetime.strptime(date_string, format)

The datetime.strptime(date_string, format) takes date_string, formats it as an argument, and returns the datetime object. This is shown below.

>>> import datetime
>>> datetime.datetime.strptime(“May 12 2018”, “%B %d %Y”)
datetime.datetime(2018, 5, 12, 0, 0)
>>> print datetime.datetime.strptime(“May 12 2018 13:03:29”, “%B %d %Y %H:%M:%S”)
2018–05–12 13:03:29

Strftime(format)

The strftime(format) is used generate the formatted date from thedatetimeobject.

>>> print datetime.datetime.now().strftime(“%d %b, %Y”)
22 Aug, 2018

ctime()

This method converts seconds to a 24-character string in the following form: “Mon Jun 20 23:21:05
1994”.

>>> datetime.datetime.now().ctime()
‘Thu Aug 23 00:07:28 2018’
>>>

isoformat()

isoformat() returns a string representing the date in ISO 8601 format, ‘YYYY-MM-DD’. For example:

datetime.date

>>> datetime.datetime.now().isoformat()
‘2018–08–23T00:11:32.393000’
>>>

datetime.date

Let’s now discuss the datetime.date class.

datetime.today()

This method returns today’s date. For example:

>>> import datetime
>>> print datetime.datetime.today()
2018–08–23 23:18:22.044000
>>>

datetime.date.fromtimestamp()

This method converts a Unix stamp or epoch to a date. For example:

>>> print datetime.date.fromtimestamp(0)
1970–01–01
>>>
>>> import time
>>> time.time()
1535047001.754
>>>
>>> print datetime.date.fromtimestamp(1535047001.754)
2018–08–23
>>>

datetime.timedelta
datetime.timedelta is used to create a time difference between two dates or times.

The datetime.timedelta class takes keyworded arguments. According to the Python docs:

Note : “All arguments are optional and default to 0. Arguments may be ints, longs, or floats, and may be positive or negative. Only days, seconds, and microseconds are stored internally. Arguments are converted to those units.”

Let’s create twp different exercises for delta.

Let’s create a time delta of 10 seconds.

>>> import datetime
>>> delta1=datetime.timedelta(seconds=10)

Next, we’ll subtract the time delta from the current time.

>>> now1 = datetime.datetime.now()
>>> now1
datetime.datetime(2018, 8, 24, 22, 53, 56, 488000)
>>> print now1
2018–08–24 22:53:56.488000
>>> print now1 — delta1
2018–08–24 22:53:46.488000

Now, we add the time delta to the current time.

>>> print now1 + delta1
2018–08–24 22:54:06.488000
>>>

Let’s do another, more complete exercise.

  1. Create a Unix time means an epoch of 10 days ago.
  2. Create a Unix time 10 days later.

Let’s go step by step

>>> import datetime
>>> import time

Create two deltas for time difference one for 10 days ago and one for 10 days later.

>>> delta1=datetime.timedelta(days=10)
>>> delta2=datetime.timedelta(days=-10)

Add both deltas to the current time.

>>> now1 = datetime.datetime.now()
>>> ten_days_ago = now1+delta2
>>>
>>> ten_days_later = now1+delta1
>>>
>>> print ten_days_ago
2018–08–14 23:09:04.861000
>>>
>>> print ten_days_later
2018–09–03 23:09:04.861000
>>>

In order to remove the use of a floating point, the strftime method has been used.

>>> date1 = ten_days_ago.strftime( “%Y-%m-%d %H:%M:%S” )
>>> date1
‘2018–08–14 23:09:04’

By the time we use the module, the Unix time or epochs have been created.

>>> int(time.mktime(time.strptime(date1, ‘%Y-%m-%d %H:%M:%S’) ) )
1534268344
>>>
>>> date2 = ten_days_later.strftime(“%Y-%m-%d %H:%M:%S”)
>>>
>>> int(time.mktime( time.strptime(date2, ‘%Y-%m-%d %H:%M:%S’) ) )
1535996344
>>>

Python Calendar Module

Now we’ll use calendar module to print the calendar of a particular month. In order to print a particular month, calendar.month(year, month) will be used as shown below.

>>> import calendar
>>> print calendar.month(2018,8)
August 2018
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
>>>

Let’s print the calendar for the 2018 year.

>>> import calendar
>>> print calendar.calendar(2018)

Consider if you want to find out whether a particular year is a leap year or not. You can use calendar.isleap(year). See the example below.

>>> calendar.isleap( 2000 )
True
>>> calendar.isleap( 2001 )
False
>>> calendar.isleap( 2016 )
True
>>> calendar.isleap( 1992 )

Consider if you want to find out the number of leap years in the range of y1 to y2. See the example below.

>>> calendar.leapdays( 1992 , 2016 )
6
>>> calendar.leapdays( 1991 , 2015 )
6
>>>

The last year is not included in the range.

Consider that you want to know the time in different countries. By default, time-related modules return the time according to your time zone. Let’s see how to get the time from a different country.

>>> import datetime
>>> import pytz

Let’s check the current time of ‘US/Eastern.’

>>> print datetime.datetime.now(pytz.timezone(‘US/Eastern’))
2018–08–25 14:25:34.712000–04:00
>>>

Let’s check the current time in Kolkata, India.

>>> print datetime.datetime.now(pytz.timezone(‘Asia/Kolkata’))
2018–08–25 23:56:40.564000+05:30

If you don’t know the name of the time zone, then you can use search the time zone using the country name.

>>> pytz.country_timezones.get(“NZ”)
[u’Pacific/Auckland’, u’Pacific/Chatham’]
>>>

New Zealand has two timezones.

Let us check the name of the timezone of India

>>> pytz.country_timezones.get(“IN”)
[u’Asia/Kolkata’]
>>>
pytz.country_timezones.keys()

The above line returns the list of country abbreviations as a shown example below.

[u’BD’, u’BE’, u’BF’, u’BG’, u’BA’, u’BB’, u’WF’, u’BL’, u’BM’, u’BN’, u’BO’, u’BH’, u’BI’, u’BJ’, u’BT’, u’JM’, u’BW’, u’WS’, u’BQ’, u’BR’, u’BS’, u’JE’, u’BY’ So on…….]

If you want to confirm whether ‘abbreviation IN’ belongs to India or other countries like Iran, you can use pytz.country_names.get( ‘IN’ ).

>>> print (pytz.country_names.get( ‘IN’ ) )
#Prints "India"

If you want to check all the countries and their abbreviations, use the following piece of code:

>>> for each in pytz.country_names.iteritems():
… print each
…
(u’BD’, u’Bangladesh’)
(u’BE’, u’Belgium’)
(u’BF’, u’Burkina Faso’)
(u’BG’, u’Bulgaria’)

Learn More

Thanks for reading !

#python

What is GEEK

Buddha Community

Python Datetime Module For Beginners
Ray  Patel

Ray Patel

1619518440

top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel

1619571780

Top 20 Most Useful Python Modules or Packages

 March 25, 2021  Deepak@321  0 Comments

Welcome to my blog, In this article, we will learn the top 20 most useful python modules or packages and these modules every Python developer should know.

Hello everybody and welcome back so in this article I’m going to be sharing with you 20 Python modules you need to know. Now I’ve split these python modules into four different categories to make little bit easier for us and the categories are:

  1. Web Development
  2. Data Science
  3. Machine Learning
  4. AI and graphical user interfaces.

Near the end of the article, I also share my personal favorite Python module so make sure you stay tuned to see what that is also make sure to share with me in the comments down below your favorite Python module.

#python #packages or libraries #python 20 modules #python 20 most usefull modules #python intersting modules #top 20 python libraries #top 20 python modules #top 20 python packages

Ray  Patel

Ray Patel

1619510796

Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Shardul Bhatt

Shardul Bhatt

1626775355

Why use Python for Software Development

No programming language is pretty much as diverse as Python. It enables building cutting edge applications effortlessly. Developers are as yet investigating the full capability of end-to-end Python development services in various areas. 

By areas, we mean FinTech, HealthTech, InsureTech, Cybersecurity, and that's just the beginning. These are New Economy areas, and Python has the ability to serve every one of them. The vast majority of them require massive computational abilities. Python's code is dynamic and powerful - equipped for taking care of the heavy traffic and substantial algorithmic capacities. 

Programming advancement is multidimensional today. Endeavor programming requires an intelligent application with AI and ML capacities. Shopper based applications require information examination to convey a superior client experience. Netflix, Trello, and Amazon are genuine instances of such applications. Python assists with building them effortlessly. 

5 Reasons to Utilize Python for Programming Web Apps 

Python can do such numerous things that developers can't discover enough reasons to admire it. Python application development isn't restricted to web and enterprise applications. It is exceptionally adaptable and superb for a wide range of uses.

Robust frameworks 

Python is known for its tools and frameworks. There's a structure for everything. Django is helpful for building web applications, venture applications, logical applications, and mathematical processing. Flask is another web improvement framework with no conditions. 

Web2Py, CherryPy, and Falcon offer incredible capabilities to customize Python development services. A large portion of them are open-source frameworks that allow quick turn of events. 

Simple to read and compose 

Python has an improved sentence structure - one that is like the English language. New engineers for Python can undoubtedly understand where they stand in the development process. The simplicity of composing allows quick application building. 

The motivation behind building Python, as said by its maker Guido Van Rossum, was to empower even beginner engineers to comprehend the programming language. The simple coding likewise permits developers to roll out speedy improvements without getting confused by pointless subtleties. 

Utilized by the best 

Alright - Python isn't simply one more programming language. It should have something, which is the reason the business giants use it. Furthermore, that too for different purposes. Developers at Google use Python to assemble framework organization systems, parallel information pusher, code audit, testing and QA, and substantially more. Netflix utilizes Python web development services for its recommendation algorithm and media player. 

Massive community support 

Python has a steadily developing community that offers enormous help. From amateurs to specialists, there's everybody. There are a lot of instructional exercises, documentation, and guides accessible for Python web development solutions. 

Today, numerous universities start with Python, adding to the quantity of individuals in the community. Frequently, Python designers team up on various tasks and help each other with algorithmic, utilitarian, and application critical thinking. 

Progressive applications 

Python is the greatest supporter of data science, Machine Learning, and Artificial Intelligence at any enterprise software development company. Its utilization cases in cutting edge applications are the most compelling motivation for its prosperity. Python is the second most well known tool after R for data analytics.

The simplicity of getting sorted out, overseeing, and visualizing information through unique libraries makes it ideal for data based applications. TensorFlow for neural networks and OpenCV for computer vision are two of Python's most well known use cases for Machine learning applications.

Summary

Thinking about the advances in programming and innovation, Python is a YES for an assorted scope of utilizations. Game development, web application development services, GUI advancement, ML and AI improvement, Enterprise and customer applications - every one of them uses Python to its full potential. 

The disadvantages of Python web improvement arrangements are regularly disregarded by developers and organizations because of the advantages it gives. They focus on quality over speed and performance over blunders. That is the reason it's a good idea to utilize Python for building the applications of the future.

#python development services #python development company #python app development #python development #python in web development #python software development

Sival Alethea

Sival Alethea

1624291780

Learn Python - Full Course for Beginners [Tutorial]

This course will give you a full introduction into all of the core concepts in python. Follow along with the videos and you’ll be a python programmer in no time!
⭐️ Contents ⭐
⌨️ (0:00) Introduction
⌨️ (1:45) Installing Python & PyCharm
⌨️ (6:40) Setup & Hello World
⌨️ (10:23) Drawing a Shape
⌨️ (15:06) Variables & Data Types
⌨️ (27:03) Working With Strings
⌨️ (38:18) Working With Numbers
⌨️ (48:26) Getting Input From Users
⌨️ (52:37) Building a Basic Calculator
⌨️ (58:27) Mad Libs Game
⌨️ (1:03:10) Lists
⌨️ (1:10:44) List Functions
⌨️ (1:18:57) Tuples
⌨️ (1:24:15) Functions
⌨️ (1:34:11) Return Statement
⌨️ (1:40:06) If Statements
⌨️ (1:54:07) If Statements & Comparisons
⌨️ (2:00:37) Building a better Calculator
⌨️ (2:07:17) Dictionaries
⌨️ (2:14:13) While Loop
⌨️ (2:20:21) Building a Guessing Game
⌨️ (2:32:44) For Loops
⌨️ (2:41:20) Exponent Function
⌨️ (2:47:13) 2D Lists & Nested Loops
⌨️ (2:52:41) Building a Translator
⌨️ (3:00:18) Comments
⌨️ (3:04:17) Try / Except
⌨️ (3:12:41) Reading Files
⌨️ (3:21:26) Writing to Files
⌨️ (3:28:13) Modules & Pip
⌨️ (3:43:56) Classes & Objects
⌨️ (3:57:37) Building a Multiple Choice Quiz
⌨️ (4:08:28) Object Functions
⌨️ (4:12:37) Inheritance
⌨️ (4:20:43) Python Interpreter
📺 The video in this post was made by freeCodeCamp.org
The origin of the article: https://www.youtube.com/watch?v=rfscVS0vtbw&list=PLWKjhJtqVAblfum5WiQblKPwIbqYXkDoC&index=3

🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!

#python #learn python #learn python for beginners #learn python - full course for beginners [tutorial] #python programmer #concepts in python