• No products in the cart.

104.1.3 Python Objects

Quick look into python objects.

Link to the previous post : https://statinfer.com/104-1-2-python-environment/

In this post we will go through some basic python objects.

Type of Objects

  • Object to refer to any entity in a python program.
  • Python has some standard built in object types
    • Numbers
    • Strings
    • Lists
    • Tuples
    • Dictionaries
  • Having a good knowledge on these basic objects is essential to fee comfortable in Python programming.

Numbers

  • Numbers: integers & floats
In [39]:
age=30
age
Out[39]:
30
In [34]:
weight=102.88
weight
Out[34]:
102.88
In [35]:
x=17
x**2 #Square of x
Out[35]:
289

Check the variable types for age and weight in variable explorer

In [37]:
type(age)
Out[37]:
int
In [38]:
type(weight)
Out[38]:
float

Strings

Strings are amongst the most popular types in Python. There are a number of methods or built-in string functions

Defining Strings

In [40]:
name="Sheldon"
msg="Data Vedi Data Science Classes"

Accessing strings

In [41]:
print(name[0])
print(name[1])
S
h
  • This is as good as substring
In [42]:
print(msg[0:9])
Data Vedi

length of string

In [44]:
len(msg)
Out[44]:
30
In [43]:
print(msg[10:len(msg)])
Data Science Classes

Displaying string multiple time

In [45]:
msg="Site under Construction"
msg*10
msg*50
Out[45]:
'Site under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under ConstructionSite under Construction'
  • There is a difference between print and just displaying a variable.
In [47]:
message="Data Science on R and Data Science on Python \n"
message*10
Out[47]:
'Data Science on R and Data Science on Python \nData Science on R and Data Science on Python \nData Science on R and Data Science on Python \nData Science on R and Data Science on Python \nData Science on R and Data Science on Python \nData Science on R and Data Science on Python \nData Science on R and Data Science on Python \nData Science on R and Data Science on Python \nData Science on R and Data Science on Python \nData Science on R and Data Science on Python \n'
In [46]:
print(message*10)
Data Science on R and Data Science on Python 
Data Science on R and Data Science on Python 
Data Science on R and Data Science on Python 
Data Science on R and Data Science on Python 
Data Science on R and Data Science on Python 
Data Science on R and Data Science on Python 
Data Science on R and Data Science on Python 
Data Science on R and Data Science on Python 
Data Science on R and Data Science on Python 
Data Science on R and Data Science on Python 

String Concatenation

In [48]:
msg1="Site under Construction "
msg2=msg1+"Go to home page \n"
print(msg2)
print(msg2*10)
Site under Construction Go to home page 

Site under Construction Go to home page 
Site under Construction Go to home page 
Site under Construction Go to home page 
Site under Construction Go to home page 
Site under Construction Go to home page 
Site under Construction Go to home page 
Site under Construction Go to home page 
Site under Construction Go to home page 
Site under Construction Go to home page 
Site under Construction Go to home page 

List

  • List is a hybrid datatype
  • A sequence of related data
  • Similar to array, but all the elements need not be of same type
  • Creating a list
In [50]:
mylist1=['Sheldon','Male', 25]
  • Accessing list elements
In [52]:
mylist1[0] #Python indexing starts from 1
Out[52]:
'Sheldon'
In [53]:
mylist1[1]
Out[53]:
'Male'
In [54]:
mylist1[2]
Out[54]:
25
  • Appending to a list
In [55]:
mylist2=['L.A','No 173', "CR108877"]
final_list=mylist1+mylist2
final_list
Out[55]:
['Sheldon', 'Male', 25, 'L.A', 'No 173', 'CR108877']
  • Updating list elements
In [56]:
final_list[2]=35
final_list
Out[56]:
['Sheldon', 'Male', 35, 'L.A', 'No 173', 'CR108877']
  • Length of list
In [57]:
len(final_list)
Out[57]:
6
  • Deleting an element in list
In [58]:
del final_list[5]
final_list
Out[58]:
['Sheldon', 'Male', 35, 'L.A', 'No 173']

Tuples

  • Also sequence data types.
  • Crated using parenthesis. Lists were created using square brackets.
  • Tuples can’t be updated.

Tuples example:

In [60]:
my_tuple=('Mark','Male', 55)
my_tuple
Out[60]:
('Mark', 'Male', 55)
In [62]:
my_tuple[1]
Out[62]:
'Male'
In [63]:
my_tuple[2]
Out[63]:
55
In [61]:
my_tuple[0]*10
Out[61]:
'MarkMarkMarkMarkMarkMarkMarkMarkMarkMark'
  • Difference between tuples and lists
In [65]:
my_list=['Sheldon','Male', 25]
my_tuple=('Mark','M', 55)

my_list[2]=30
my_list
Out[65]:
['Sheldon', 'Male', 30]
In [68]:
my_tuple[2]=40
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-68-c6d510f4ba89> in <module>()
----> 1 my_tuple[2]=40

TypeError: 'tuple' object does not support item assignment

Dictionaries

  • Dictionaries have two major element types key and Value.
  • Dictionaries are collection of key value pairs
  • Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.
  • Keys are unique within a dictionary
In [71]:
city={0:"LA", 1:"PA" , 2:"FL"}
city
Out[71]:
{0: 'LA', 1: 'PA', 2: 'FL'}
In [72]:
city[0]
Out[72]:
'LA'
In [73]:
city[1]
Out[73]:
'PA'
In [70]:
city[2]
Out[70]:
'FL'
  • In dictionary, keys are similar to indexes. We define our own preferred indexes in dictionaries.
  • Make sure that we give the right key index while accessing the elements in dictionary.
In [74]:
names={1:"David", 6:"Bill", 9:"Jim"}
names
Out[74]:
{1: 'David', 6: 'Bill', 9: 'Jim'}
In [82]:
names[0] #Doesn't work, because we haven't assign "0" to any value?
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-82-2bdf624c7c2b> in <module>()
----> 1 names[0] #Doesn't work, because we haven't assign "0" to any value?

KeyError: 0
In [77]:
names[1]
Out[77]:
'David'
In [79]:
names[2]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-79-5fc2e5d7f092> in <module>()
----> 1 names[2]

KeyError: 2
In [80]:
names[6]
Out[80]:
'Bill'
In [81]:
names[9]
Out[81]:
'Jim'
  • In the key value pairs, key doesn’t always need to be a number.
In [83]:
edu={"David":"Bsc", "Bill":"Msc", "Jim":"Phd"}
edu
Out[83]:
{'Bill': 'Msc', 'David': 'Bsc', 'Jim': 'Phd'}
In [86]:
edu[0]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-86-097da42662a8> in <module>()
----> 1 edu[0]

KeyError: 0
In [87]:
edu[1]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-87-86fe0fd73d3a> in <module>()
----> 1 edu[1]

KeyError: 1
In [88]:
edu[David]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-88-9c7944aa3a3d> in <module>()
----> 1 edu[David]

NameError: name 'David' is not defined
In [89]:
edu["David"]
Out[89]:
'Bsc'
  • Updating values in dictionary
In [90]:
edu
Out[90]:
{'Bill': 'Msc', 'David': 'Bsc', 'Jim': 'Phd'}
In [91]:
edu["David"]="MSc"
edu
Out[91]:
{'Bill': 'Msc', 'David': 'MSc', 'Jim': 'Phd'}
  • Updating keys in dictionary.
  • Delete the key and value element first and then add new element.
In [94]:
city={0:"LA", 1:"PA" , 2:"FL"}
#How to make 6 as "LA"
del city[0]
city
Out[94]:
{1: 'PA', 2: 'FL'}
In [93]:
city[6]="LA"
city
Out[93]:
{1: 'PA', 2: 'FL', 6: 'LA'}
  • Fetch all keys and all values separately
In [96]:
city.keys()
Out[96]:
dict_keys([1, 2])
In [95]:
city.values()
Out[95]:
dict_values(['PA', 'FL'])
In [97]:
edu.keys()
Out[97]:
dict_keys(['Jim', 'Bill', 'David'])
In [98]:
edu.values()
Out[98]:
dict_values(['Phd', 'Msc', 'MSc'])

 

Next post is on conditional operators in python.
Link to the next post : https://statinfer.com/104-1-4-conditional-operators-in-python/

Statinfer

Statinfer derived from Statistical inference. We provide training in various Data Analytics and Data Science courses and assist candidates in securing placements.

Contact Us

info@statinfer.com

+91- 9676098897

+91- 9494762485

 

Our Social Links

top
© 2020. All Rights Reserved.