Friday, November 10, 2017

Learning Python

Some of simple new features in Python that are not exist in C:

Getting integer result with devision :

x = 50 / 6  = > x = 8.333  (float value)

If we want integer operation try with "//" mark.

x = 50//6 = 8


int(A) => returns error 


Name = "Michael Jackson"

Name[0]: M
Name[14] : n

We can also use negative indexing for strings.

Name[-15]:M
Name[-1]: n


String Slicing:
Name[0:4] = Mich
Name[8:12]= Jack

String Stride:



Name[::2] = "McalJcsn"  => 2 indicates every 2nd variable.


String Stride and Slice:


Namw[0:5:2]="Mca"

0:5 means = [0:5) = It includes 0 and excludes 5. So consider only 0 to 4 .

[0:5:2] means Slice with 0 to 4 and stride with every 2nd variable with that slice limit.


Tuples :
Tuples are written as comma separated elements within parentheses.

Tuples are immutable.   



Ratings[2] = 4 gives error. Tuples are immutable . We can not change values in Tuples.

Due to immutable property of Tuple , if we want to manipulate tuple we need to create new Tuple.


Lists:

Lists are mutable. Lists are represented in Square Brackets.

Extend method will add as separate elements in the final list. See "pop" and 10 become separate elements in the list.

Append method will add as list as single element. See ["pop" ,10] becomes a list in the list at 3rd position.

List cloning:
A = [ "Cloning" , test , 10 , 2.1]
B = A   => Both A and B referring to the same list . Both are having same reference.

B = A [ : ]

Here Both A and B having same list elements because B formed by new copy of the elements from A. 


Sets:
Sets are created using Curly Brackets. 


Dictionaries:

dir(NameofObject): Will return the list of attributes in that class. 

1 comment: