Roshan Book

My Tech Notebook

Python tutorial 5 Variables Objects and Values

Everything in python is an object

All object has an ID, Type and Value

Mutable object change values – Lists, dictionaries

Immutable object does not change values – numbers, strings, tuples

# Checking ID, Value and Type of a variable

>>> x =42
>>> id(x)
>>> type(x)
<class ‘int’>

# Numbers

int and float

num=42//9 , gives integer answer and not rounded up – 4

num = round(42/9, 2) – round to 2 places of decimal- 5

num= 42%9 – reminder 6

# Strings

print (r“this is n string”) – adding r removes any backslash effect and print n as it is

Printing variable

s=”This is a {} string”.format(n)

format is a method in string object

‘’’ ‘’’ – triple quotes helps to print multiple lines


x=(1,2,3) – immutable . you cannot append things



x= [1,2,3] – mutable. You can add, change, delete in Lists

# Dictionary

x = {‘one’:1, ‘two’:2}

x==y compare their values

x is y -check if id is same

One response to “Python tutorial 5 Variables Objects and Values

  1. xrumer October 31, 2011 at 11:10 am

    Pretty element of content. I simply stumbled upon your web site and in accession capital to claim that I get actually loved account your blog posts. Any way I’ll be subscribing on your feeds or even I achievement you get right of entry to consistently quickly.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: