Welcome

sampleqa.in tutorials, python tutorial

Python tutorial


Data Types

      Python Data Types or Python Built-in Object Types

  • Numbers
  • Strings
  • Unicode Strings
  • Lists
  • Dictionary
  • Tuple
  • Files
  • Sets
  • Bytes, Bytearrays

Python Numbers

       There are 3 distinct numeric types: Integer, float,complex numbers

boolean is a subtype of Integer

Integer are numbers without fractional part.

Float Data type represents numbers with fractional part.

Integer represents whole numbers,signed and unsigned 0,1,10, -1000 etc., Upper Limit, Lower Limit are system dependent.

Float represents numbers with fractional part,signed and unsigned 10.0000001,-10.100001, 0.0 etc., Upper Limit, Lower Limit are system dependent.

Boolean is a subtype of integer, accepts only 2 values , True or False

Boolean variables usage

>>> b=False
>>> b
False
>>> print(b)
False
>>> 
>>> b=True
>>> b
True
>>> print(b)
True
>>> 

 Using bool() built-in function
>>>b=bool()
>>>b
False
>>>b=True
>>>b
True

>>>b=bool(True)
>>>b
True

	
	

Python Strings

     Python Strings are Unicode Characters. Unicode characters supports english as well as other international characters. Python Strings should be sorrounded by Quotation marks. Quotes can be

  • Single Quotes ''
  • Double Quotes ""
  • Triple Single Quotes or Double Quotes """string""" or '''string'''
Strings are immutable , you cannot modify string content or add to a string once it is created. but new strings can be created using existing strings.

Creating a String

   Unlike other Programming languages, Python Strings can be single quoted or double Quoted.

		s = 'Programmer'
		s =  "Programmer"
		s = str('Programmer')
	
>>> s='Programmer'
>>> s
'Programmer'
>>> s="Programmer"
>>> s
'Programmer'
>>> s=str('Programmer')
>>> s
'Programmer'
		
Double Quotes and single Quotes can be mixed together

>>> s = 'Sam says "You got phone call"'
>>> s
'Sam says "You got phone call"'
>>> print(s)
Sam says "You got phone call"

Escape Characters in the String

     here \n \r \t are escape characters

>>> s = "This line has 2 sentenses  \n line 1 \n line2\r"
>>> print(s)
This line has 2 sentenses  
 line 1 
 line2

Tabs in the String

>>> s='\ts\tp\ta\tm'
>>> s
'\ts\tp\ta\tm'
>>> print(s)
	s	p	a	m
>>> 	
	

Unicode Characters in the String

>>> s = '\u21c7\u21cb\u27b5\u2764'
>>> s
'⇇⇋➵❤'
>>> print(s)
⇇⇋➵❤
>>> 

Please refer for String slicing

Python Tuple

     Tuples are another kind of sequence.Tuples consists of series of values(or variables) seperated by commas, optionally values are surrounded by parenthesis. Tuples accept any kind of objects Tuples are immutable, so you cannot modify or extend once it is created.

Creating a tuple

	1. t = (1,2,3,4,5)
	2. t= 1,2,3,4,5
	3. t = tuple()

There are 3 ways to create a tuple Object.
1. t = (1,2,3,4,5) creates a tuple object with values 1,2,3,4,5
2. t = tuple() creates a empty tuple object by calling python built-in function dict(), i.e it has zero elements

>>> t = (1,2,3,4,5)
>>> t
(1,2,3,4,5)

//Creating a empty tuple

>>> t=tuple()
>>> t
()
    

Python Dictionary

     Python Dictionary, stores information in key-value pair. They are unordered table that map keys to values. Keys are immutable,so keys can be number,string or tuple. Values are mutable, so it can be of any type.

Dictionary literals are written as a comma-seperated series of key:value pairs inside curly braces.

Creating a Dictionary

	1. d = {1:'one', 2:'two', 3:'three', 4:'four', 5:'five'}
	2. d = dict()

There are 2 ways to create a Dictionary Object.
1. d = {1:'one', 2:'two', 3:'three', 4:'four', 5:'five'} creates a Dictionary object with keys 1,2,3,4,5 corresponding values are 'one', 'two', 'three', 'four', 'five'
2. d=dict() creates a empty Dictionary object by calling python built-in function dict(), i.e it has zero elements

>>> d = {1:'one', 2:'two', 3:'three', 4:'four', 5:'five'}
>>> d
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}


//Creating a empty Dictionary

>>> d=dict()
>>> d
{}
    

Python Sets

     Python Sets are like Dictionaries with keys but no corresponding values. Set literals are expressed in curly brackes, values are seperated by comma. Set values must be immutable and unique.

Creating a Set

	1. s = {1,2,3,4,5,5}
	2. s = set()

There are 2 ways to create a set.
1. s={1,2,3,4,5,5} creates a set object with values 1,2,3,4,5
2. s=set() creates a empty set object by calling python built-in function set(), i.e it has zero elements

>>> s={1,2,3,4,5,5}
>>> s
{1, 2, 3, 4, 5}

//Creating a empty Set

>>> s=set()
>>> s
{}
    

Python Lists

     Python Lists are another kind of sequence object. A Literal list consists of comma seperated values enclosed in square brackets. values can be any type of objects, unlike tuples, List can be extended or modified. it is a mutable object.

Creating a List

	1. l = [1,2,3,4,5]
	2. l = list()

There are 2 ways to create a List.
1. l=[1,2,3,4,5] creates a list object with values 1,2,3,4,5
2. l=list() creates a empty list object by calling python built-in function list(), i.e it has zero elements

>>> l=[1,2,3,4,5]
>>> l
[1, 2, 3, 4, 5]

//Creating a empty List

>>> l=list()
>>> l
[]
    

ADS