Python Basics | Variables and Data types

To Create a variable called name

>>Name= “aashish”
>> Name
Aashish
Note: Name is not type associated instead aashish is object and string type is associated to object

To know exact location or reference of name
id(name)

To hex  address

hex(id(name))

Name= 'aashish\n mittal’ # escape sequence
Print name

Name= r’aashsish\n mittal’.  #Called raw string g escape sequence does not work

Name= “””
...aashish mittal is your
…is your instructor
…”””

Try print name # will print in same way as it initialized

Unicode string(used for internationalization)
>>Name= u’aashish’
>>Name

To print Unicode string
>>> Name

To convert Unicode into string
Str(name)

To convert regular string into Unicode
Unicode(“aashish”)


String operations  string s are by default immutable (cannot be change)
>>Name=”aashish”
>>>Name[0]=”b”# not possible

To concatenate
Fname=”aashish”
Lname=”mittal”
Comple_name=Fname+’  ‘+Lname

Garbage collection supported by Python like another high level language

Repeatedly string
Buffer=”A”*50
Buffer


Slicing string
Name=”aashish mittal”
Name[5:10]. # a is at 0

Name[5:10:2]# step of 2

Int(“42”)# int function to convert string to int
Str(42) # vice versa
String methods
String.fInd()

Name=”aashish mittal “
Name.find(“Gupta”) # will return -1

Name.find(“mittal”). #Will return position of m
String.replace()

new_Name=Name.replace ('aaashish’,’ aashish’) # name is never going to modify is immutable

String.split()
Name.split()

Name.split('a’)

String formatting
Ip=”192.168.1.5”
Line=”Find Mac of given IP %s”%ip

Try line
port="80"
Line=”Find Mac of given IP:. %s and open  %s deatils” %(ip,port)

#Variables and Datatypes
Operators
All like c  #A and b a or b


#Type cating in Python
a=20
b=68
print float(a)/float(b)
print pow(2,3)  # Math function

#To take input from user
 a=int(raw_input("Enter value of a"));
b=int(raw_input("Enter value of b"));
c=a+b
print"Sum of %d+%d= "%(a,b,c)



Reactions: