EmbLogic's Blog

Python

Python is a very simple language, and has a very straightforward syntax. It encourages programmers to program without prepared code. The simplest directive in Python is the “print” directive – it simply prints out a line and also includes a newline, unlike in C

There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. This tutorial uses Python 2, because it is more widely used and supported.

 To print a string, just write:

print "hello"

Indentation

Python uses indentation for blocks, instead of curly braces. Both tabs and spaces are supported, but the standard indentation requires standard Python code to use four spaces. For example:

x = 1

      if x == 1: # tab for indentation print "x is 1." this print statement belongs to the block of above specified if block and every statement written after indentation will come inside the “if” block.

Variables and Types

Python is completely object oriented. You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.

5 major variable in python

Numbers

Python supports two types of numbers – integers and floating point numbers. (It also supports complex numbers,

To define an integer, use the following syntax:

myint = 7 To define a floating point number, you may use one of the following notations: myfloat = 7.0

Strings

Strings are defined either with a single quote or a double quotes.

mystring = 'hello' mystring = "hello"

Lists

Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner. The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. Good thing about a list is that items in a list need not all have the same type.

mylist = [“hello”,123]

Tuples

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be changed i.e., tuples are immutable and tuples use parentheses and lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values and optionally you can put these comma-separated values between parentheses also. For example:

tup=(“hello”,123)

Dictionaries

A dictionary is mutable and is another container type that can store any number of Python objects, including other container types. Dictionaries consist of pairs (called items) of keys and their corresponding values.

Python dictionaries are also known as associative arrays or hash tables. The general syntax of a dictionary is as follows:

dict = {“name” : “xyz” , “age” : “123”}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>