Python Data-types:
As we know that there are keywords to identify the variables_name that they are int, float or string.
Only by assigning values in variable_names the data-type is defined automatically.
In Python there are five standard datat-ypes
They are listed as follows:
- Numbers
- String
- List
- Tuple
- Dictionary
Number
Python supports four different numerical types:- int (signed integers)
- long (long integers [can also be represented in octal and hexadecimal])
- float (floating point real values)
- complex (complex numbers)
var1=10 # int var2=30.3 # float var4=51924361L # long var5=3.14j # complex
Note
Complex numbers are of the form
a+ib # where a is real part and b is imaginary part
Strings:
Strings are identified as a contiguous set of characters in between quotation marks.Python allows for either pairs of single or double quotes.
Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.
Note
The plus ( + ) sign is the string concatenation operator, and the asterisk ( * ) is the repetition operator. For example:str = 'Hello World' print str # Prints complete string print str[0] # Prints first character of the string print str[2:5] # Prints characters starting from 3rd to 5th print str[2:] # Prints string starting from 3rd character print str * 2 # Prints string two times print str + "_string_2# " Prints concatenated string
Output
Hello World H llo llo World Hello WorldHello World Hello World_string_2
Python Lists:
Lists are the most versatile of Python's compound data types.List contains items separated by commas and enclosed within square brackets ([]).
The values stored in a list can be accessed using the slice operator ( [ ] and [ : ]).
The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator.
Example:
movies = ["The Holy Grail", "The Life of Brian", "The Meaning of Life", "Awatar", "Transformer"]
list = [12345, 'andru']
print movies # Prints complete list print movies[0] # Prints first element of the list print movies[0:2] # Prints elements starting from first till 3rd print movies[2:] # Prints elements starting from 3rd element print list * 2 # Prints list two times print movies + list # Prints concatenated lists
Output
["The Holy Grail", "The Life of Brian", "The Meaning of Life"] The Holy Grail ["The Holy Grail", "The Life of Brian", "The Meaning of Life"] ["Awatar", "Transformer"] [12345, 'andru'][12345, 'andru'] ["The Holy Grail", "The Life of Brian", "The Meaning of Life", "Awatar", "Transformer", 12345, "andru"]
Python Dictionary
Python's dictionaries are kind of hash table type.
They consist of key-value pairs.
Dictionary keys can be almost any Python type, but are usually numbers or strings.
Note
Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ( [] ).Example:
dict = {} dict['one'] = "This is one" dict[2] = "This is two" dict_2 = {'name': 'john','code':6734, 'dept': 'sales'} print dict['one'] # Prints value for 'one' key print dict[2] # Prints value for 2 key print dict_2 # Prints complete dictionary print dict_2.keys() # Prints all the keys print dict_2.values() # Prints all the values
Output
This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']
var1=100 var2=30.3 var3="scanftree" var4=51924361L print var1 print var2 print var3 print var4
In the next chapter you will to know basic operators python