New Module Python 3.x Version ≥ 3.2 import datetime dt = datetime.datetime.strptime("2016-04-15T08:27:18-0500", "%Y-%m-%dT%H:%M:%S%z") For other versions of Python, you can use an external library such as dateutil, which makes parsing a string with timezone into a datetime object is quick. import dateutil.parser dt = dateutil.parser.parse("2016-04-15T08:27:18-0500") The dt variable is now a datetime object with the following value: datetime.datetime(2016, 4, 15, 8, 27, 18, tzinfo=tzoffset(None, -18000)) Section 5.2: Constructing timezone-aware datetimes By default all datetime objects are naive. To make them timezone-aware, you must attach a tzinfo object, which provides the UTC offset and timezone abbreviation as a function of date and time. Fixed Offset Time Zones For time zones that are a fixed offset from UTC, in Python 3.2+, the datetime module provides the timezone class, a concrete implementation of tzinfo, which takes a timedelta and an (opt...
Posts
A module is a file containing Python definitions and statements. Function is a piece of code which execute some logic. >>> pow(2,3) #8 To check the built in function in python we can use dir(). If called without an argument, return the names in the current scope. Else, return an alphabetized list of names comprising (some of) the attribute of the given object, and of attributes reachable from it. >>> dir(__builtins__) [ 'ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt...
Interactive input To get input from the user, use the input function (note: in Python 2.x, the function is called raw_input instead, although Python 2.x has its own version of input that is completely different): Python 3.x Version ≥ 3.0 name = input("What is your name? ") # Out: What is your name? The remainder of this example will be using Python 3 syntax. The function takes a string argument, which displays it as a prompt and returns a string. The above code provides a prompt, waiting for the user to input. name = input("What is your name? ") # Out: What is your name? If the user types "Bob" and hits enter, the variable name will be assigned to the string "Bob": name = input("What is your name? ") # Out: What is your name? Bob print(name) # Out: Bob Note that the input is always of type str, which is important if you want the user to enter numbers. Therefore, you need to convert the str before trying to us...
SETS A set is a collection of elements with no repeats and without insertion order but sorted order. They are used in situations where it is only important that some things are grouped together, and not what order they were included. For large groups of data, it is much faster to check whether or not an element is in a set than it is to do the same for a list. Defining a set is very similar to defining a dictionary: first_names = {'Adam', 'Beth', 'Charlie'} Or you can build a set using an existing list: my_list = [1,2,3] my_set = set(my_list) Check membership of the set using in: if name in first_names: print(name) You can iterate over a set exactly like a list, but remember: the values will be in an arbitrary, implementation defined order. with other sets # Intersection {1, 2, 3, 4, 5}.intersection({3, 4, 5, 6}) # {3, 4, 5} or {1, 2, 3, 4, 5} & {3, 4, 5, 6} # {3, 4, 5} # Union {1, 2, 3, 4, 5}...
Dictionaries A dictionary in Python is a collection of key-value pairs. The dictionary is surrounded by curly braces. Each pair is separated by a comma and the key and value are separated by a colon. Here is an example: state_capitals = { 'Arkansas': 'Little Rock', 'Colorado': 'Denver', 'California': 'Sacramento', 'Georgia': 'Atlanta' } To get a value, refer to it by its key: ca_capital = state_capitals['California'] You can also get all of the keys in a dictionary and then iterate over them: for k in state_capitals.keys(): print('{} is the capital of {}'.format(state_capitals[k], k)) Dictionaries strongly ...
Tuples A tuple is similar to a list except that it is fixed-length and immutable. So the values in the tuple cannot be changed nor the values be added to or removed from the tuple. Tuples are commonly used for small collections of values that will not need to change, such as an IP address and port. Tuples are represented with parentheses instead of square brackets: ip_address = ('10.20.30.40', 8080) The same indexing rules for lists also apply to tuples. Tuples can also be nested and the values can be any valid Python valid. A tuple with only one member must be defined (note the comma) this way: one_member_tuple = ('Only member',) or one_member_tuple = 'Only member', # No brackets or just using tuple syntax one_member_tuple = tuple(['Only member'])
The list type is probably the most commonly used collection type in Python. Despite its name, a list is more like an array in other languages, mostly JavaScript. In Python, a list is merely an ordered collection of valid Python values. A list can be created by enclosing values, separated by commas, in square brackets: list = [123,'abcd',10.2,'d'] #can be an array of any data type or single data type. list1 = ['hello','world'] print(list) #will output whole list. [123,'abcd',10.2,'d'] print(list[0:2]) #will output first two element of list. [123,'abcd'] print(list1 * 2) #will gave list1 two times. ['hello','world','hello','world'] print(list + list1) #will gave concatenation of both the lists. [123,'abcd',10.2,'d','hello','world'] int_list = [1, 2, 3] string_list = ['abc', 'defghi'] A list can be empty: empty_list = [] The el...
Built-in Types Booleans bool: A boolean value of either True or False. Logical operations like and, or, not can be performed on booleans. x or y # if x is False then y otherwise x x and y # if x is False then x otherwise y not x # if x is True then False, otherwise True In Python 2.x and in Python 3.x, a boolean is also an int. The bool type is a subclass of the int type and True and False are its only instances: issubclass(bool, int) # True isinstance(True, bool) # True isinstance(False, bool) # True If boolean values are used in arithmetic operations, their integer values (1 and 0 for True and False) will be used to return an integer result: True + False == 1 # 1 + 0 == 1 True * True == 1 # 1 * 1 == 1 Numbers int: Integer number a = 2 b = 100 c = 123456789 d = 38563846326424324 Integers in Python are of arbitrary sizes. Note: in older versions of Python, a long type was available and this was distinct from int. The two have been unif...
To create a variable in Python, all you need to do is specify the variable name, and then assign a value to it <variable name> = <variable_value> Python uses = to assign values to variables. There's no need to declare a variable in advance (or to assign a data type to it), assigning a value to a variable itself declares and initializes the variable with that value. There's no way to declare a variable without assigning it an initial value. Rules for variable naming: 1. Variables names must start with a letter or an underscore. 2. The remainder of your variable name may consist of letters, numbers and underscores. 3. Names are case sensitive. Even though there's no need to specify a data type when declaring a variable in Python, while allocating the necessary area in memory for the variable, the Python interpreter automatically picks the most suitable built-in type for it: You can assign multiple values to multipl...
Python is a widely used high-level programming language for general-purpose programming, created by Guido van Rossum and first released in 1991. Python features a dynamic type system and automatic memory management and supports multiple programming paradigms, including object-oriented, imperative, functional programming, and procedural styles. It has a large and comprehensive standard library. Two major versions of Python are currently in active use: Python 3.x is the current version and is under active development. Python 2.x is the legacy version and will receive only security updates until 2020. No new features will be implemented. Note that many projects still use Python 2, although migrating to Python 3 is getting easier. You can download and install either version of Python here . See Python 3 vs. Python 2 for a comparison between them. In addition, some third-parties offer re-packaged versions of Python that add commonly used libraries and other features t...
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR2weVBrPOjCFsteMkCtGTBjVAPqCLxsrBj5DeOxnyoQLWcoc1tFCKHOjZvlSeRA5l54dzyKo266zhHhO9BEdInEn8gjoFmE_GVJHvkOF8AP9WMK7zOvRJqIQMiD0hm7NAFyTx3sLCQOgD/s200/hsware-header-icon_0.png)
Application Areas: Python is most widely used for the purpose of: Scientific Areas : Scientific Applications Scipy -Solves Computer Scienceand Engineering Tasks Numpy - Multi-dimensional Array and Matrices Matplotlib - plotting Library pandas - High performance, easy to use data structures and data-analysis tool Sympy - Symbolic Mathematics library Web Development: Web Development Frameworks: DJANGO, Flask... Inbuilt Support Tools - HTML,XML,FTP... Task Runners: Celery Other Widely Used Libraries: Requests,Beautiful Soup,RSS e.t.c.. Computer Vision Computer Vision Computer Vision is Concentrated on Automatic Extraction,Analysis and Understandingof Useful Informationfrom single image Packages - Simple CV,Open CV,SciKit-Images....e.tc... Game development: Gaming Application Pygame Turtle Panda3D Belnder Some Examples Are...
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjygjpFP2onljxE9upJlTCztwLRihZUHw7l0609zoqg9EDY2OQyt9WK-A1u0RdXWn-CZyrS4Jbg7JA_QDUXoty6Ooyuaj-Rs8nbsNXZHFl4qAvaxLzyIV6Nx8tlbZxmLhmjUI31BxqFhEn8/s320/python-Programming-Language-Software.jpg)
INTRODUCTION TO PYTHON: Python was developed by “Guido van Rossum”. Also consider as “Father of python”. In 1989 at National Research institute (NRI)in Netherland. In 1980’s Guido vas Rossum was working on amoeba distributed operating system group. The name python was adopted from the some series “monthly python’s Flying circus” WHY PYTHON?? Python has some unique features they are: Easy to learn Clean syntax Comprehensive standard Library Excellent documentation General purpose Features Of Python: Simple and easy to learn: Python is a simple programming language. When we read Python program,we can feel like reading English statements. The syntax's are very simple and only 30+ keywords are available.When compared with other languages, we can write programs with very less number offline. Hence more readability and simplicity.We can reduce development and cost of the project. Freeware and Open So...