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)
506091328
>>> 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

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

format is a method in string object

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

#Tuple

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

 

#List

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

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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: