Posts

Python Opening and Closing Files

Opening and Closing Files Until now, you have been reading and writing to the standard input and output. Now, we will see how to use actual data files. Python provides basic functions and methods necessary to manipulate files by default. You can do most of the file manipulation using a file object.

Mudules

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code. Example The Python code for a module named aname normally resides in a file namedaname.py. Here is an example of a simple module, support.py − def print_func( par ):    print "Hello : ", par    return

Functions

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. def functionname( parameters ):    "function_docstring"    function_suite    return [expression] #!/usr/bin/python3 # Function definition is here def printme( str ):    "keen infotect"    print (str)    return # Now you can call printme function printme()

Date and Time

A Python program can handle date and time in several ways. Converting between date formats is a common chore for computers. Python's time and calendar modules help track dates and times. #!/usr/bin/python3 import time;      # This is required to include time module. ticks = time.time() print ("Number of ticks since 12:00am, January 1, 1970:", ticks)

Strings

Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable. For example − var1 = 'Hello World!' var2 = "Python Programming"

Numbers

Number data types store numeric values. They are immutable data types. This means, changing the value of a number data type results in a newly allocated object. Number objects are created when you assign a value to them. For example − var1 = 1 var2 = 10

Python 3 Dictionary

Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}. Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. dict = {'Name': 'Keen', 'Age': 2009, 'Class': 'Summer Training'} print ("dict['Name']: ", dict['Name']) print ("dict['Age']: ", dict['Age'])

Python 3 Tuple

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The main difference between the tuples and the lists is that the tuples cannot be changed unlike lists. Tuples use parentheses, whereas lists use square brackets. Creating a tuple is as simple as putting different comma-separated values. Optionally, you can put these comma-separated values between parentheses also. For example − tup1 = ('Keeninfotech', 'udaipur',  2009) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", "b", "c", "d"

Python 3 - Lists

Python Lists The list is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type. Creating a list is as simple as putting different comma-separated values between square brackets. For example − list1 = ['Keen', 'Infotech', 'Udaipur', 2009]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"];

Decision-making

Decision-making is the anticipation of conditions occurring during the execution of a program and specified actions taken according to the conditions. The IF statement is similar to that of other languages. The if statement contains a logical expression using which the data is compared and a decision is made based on the result of the comparison. An else statement can be combined with an if statement. An else statement contains a block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The import Statement When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module.

Python Variables Data types operators

Python Variables A variable is a named location used to store data in the memory. It is helpful to think of variables as a container that holds data which can be changed later throughout programming keen=5; keen=5.5 training='keeninfotech' a, b, c = 5, 3.2, "Hello" x = y = z = "same" Data types in Python Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. Type Conversion: The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. Python has two types of type conversion. Implicit Type Conversion Explicit Type Conversion Python Input, Output and Import print("Welcome python keen infotech"); name=input("Enter the name"); print(name); What are operators in python Arithmetic operators + Add two operands or unar...

What Is Python

What Is Python Python is a powerful programming language ideal for scripting and rapid application development. It is used in web development (Django ), scientific and mathematical computing (Orange, SymPy, NumPy) to desktop graphical user Interfaces (Pygame, Panda3D). This tutorial introduces you to the basic concepts and features of Python 3. After reading the tutorial, you will be able to read and write basic Python programs, and explore Python in depth on your own. How to Get Started With Python Python is a cross-platform programming language, meaning, it runs on multiple platforms like Windows, MacOS, Linux and has even been ported to the Java and .NET virtual machines. It is free and open source 1) Run Python 2)Run Python in the Integrated Development Environment (IDE) Python Keywords Keywords are the reserved words in Python. All the keywords except True, False and None are in lowercase and they must be written as it is. The list of all the keywords is given...