Python aims to be consistent and straightforward in the design of its syntax. The best advantage of this language is that it can dynamically set the variable types depending on the values' types as set for variables.
Python has a wide range of data types, like many simple programming languages:
number
,string
,list
,tuple
dictionary
.
Numbers
Standard Python methods are used to create the numbers:
var = 1234 #Creates Integer number assignment var = 'George' #Creates String type var
Python can automatically convert types of the number from one type to another. Type can also be defined explicitly.
int a = 10 long a = 123L float a = 12.34 complex a = 3.23J <code> **String**\\ To define Strings, use eclosing characters in quotes. Python uses single quotes ', double " and triple """ to denote strings. <code Python> Name = "George' lastName = "Smith" message = """this is the string message which is spanning across multiple lines."""
List
The list contains a series of values. To declare list variables, use brackets []
.
A = [] #Blank list variable B = [1, 2, 3] #List with 3 numbers C = [1, 'aa', 3] #List with different types
The list indexing is zero-based. Data can be assigned to a specific element of the list using an index into the list.
mylist[0] = 'sasa' mylist[1] = 'wawa' print mylist[1]
Lists aren't limited to a single dimension.
myTable = [[],[]]
In a two-dimensional array, the first number is always the row number, while the second is the column number.
Tuple
Python Tuples are defined as a group of values like a list and can be processed similarly. When assigned, Tuples got the fixed size. In Python, the fixed size is immutable. The lists are dynamic and mutable. To define Tuples, parenthesis ()
must be used.
TestSet = ('Piotr', 'Jan', 'Adam')
Dictionary
The list of key-value pairs defines a Dictionary
in Python. This data type holds related information that can be associated with keys. The Dictionary extracts a value based on the key name. Lists use the index numbers to access its members when dictionaries use a key. Dictionaries generally are used to sort, iterate and compare data.
To define the Dictionaries, the braces ({})
are used with pairs separated by a comma (,
) and the key values associated with a colon (:). Dictionaries Keys must be unique.
box_nbr = {'Alan': 111, 'John': 222} box_nbr['Alan'] = 222 #Set the associated 'Alan' key to value 222' print (box nbr['John']) #Print the 'John' key value box_nbr['Dave'] = 111 #Add a new key 'Dave' with value 111 print (box_nbr.keys()) #Print the keys list in the dictionary print ('John' in box_nbr) #Check if 'John' is in the dictionary #This returns true
All variables in Python hold references to objects and are passed to functions. Function can't change the value of variable references in its body. The object's value may be changed in the called function with the “alias”.
>>> alist = ['a', 'b', 'c'] >>> def myfunc(al): al.append('x') print al >>> myfunc(alist) ['a', 'b', 'c', 'x'] >>> alist ['a', 'b', 'c', 'x']