Roshan Book

My Tech Notebook

Tag Archives: python string

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

Python tutorial 1 strings

This program has

a) input method

b) convert to int method

c) if else syntac

 

print(“Hello”);

g= input(“enter your guess”); # input is in string format
guess=int(g); # This converts input into integer

if guess==5:
    print(“You win”)
else :
    print(“You loose”)

%d bloggers like this: