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:

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary

Number

Python supports four different numerical types:

  1. int (signed integers)
  2. long (long integers [can also be represented in octal and hexadecimal])
  3. float (floating point real values)
  4. complex (complex numbers)

  1. var1=10 # int
  2. var2=30.3 # float
  3. var4=51924361L # long
  4. 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:

  1. str = 'Hello World'
  2.  
  3. print str # Prints complete string
  4. print str[0] # Prints first character of the string
  5. print str[2:5] # Prints characters starting from 3rd to 5th
  6. print str[2:] # Prints string starting from 3rd character
  7. print str * 2 # Prints string two times
  8. 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:

  1. movies = ["The Holy Grail", "The Life of Brian", "The Meaning of Life", "Awatar", "Transformer"]
  2.  
  3. list = [12345, 'andru']
  4.  
  5.  
  6. print movies # Prints complete list
  7. print movies[0] # Prints first element of the list
  8. print movies[0:2] # Prints elements starting from first till 3rd
  9. print movies[2:] # Prints elements starting from 3rd element
  10. print list * 2 # Prints list two times
  11. 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:

  1. dict = {}
  2. dict['one'] = "This is one"
  3. dict[2] = "This is two"
  4.  
  5. dict_2 = {'name': 'john','code':6734, 'dept': 'sales'}
  6.  
  7.  
  8. print dict['one'] # Prints value for 'one' key
  9. print dict[2] # Prints value for 2 key
  10. print dict_2 # Prints complete dictionary
  11. print dict_2.keys() # Prints all the keys
  12. 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']
 

  1. var1=100
  2. var2=30.3
  3. var3="scanftree"
  4. var4=51924361L
  5.  
  6. print var1
  7. print var2
  8. print var3
  9. print var4

In the next chapter you will to know basic operators python