Posts

All Tips!

Else in for: When having a condition in loop block, we may need to have a variable to set either the condition has been passed or not. Instead of that, Python provides "else" keyword to handle that. Else will only be executed, when the loop is executed completely without breaking out using "break" statement. eg., Finding the prime number in the range eg., This can be done as follows for n in range (2,10):     for x in range (2,n):          if n%x == 0:          break else:     print (" n is prime);

Control Flows:

Spl in Python!

Multiple assignments in a line! eg., a, b = 0, 1 conditions: All non-zeros are true, zeros are false if it is sequence, non-zero is true, empty seq is false eg., myList =[1,3,5] #true myEmptyList=[] # false

List!

List is a mutable type! List can hold different types! List can hold list itself! Works similar to string slicing! myList=[5,10,15,25,30] print(myList) # [5,10,15,25,30] myList[3]=20 print(myList)# [5,10,15,20,25,30] Nested Lists: myNestedList=[[4,2,1],[38,29,19]] print(myNestedList[1][1]) #picks up 1st Index list and then 1st index element

More on Strings!

Strings are str objects in Python and can be created using str constructor Strings are immutable , which means that, strings can be changed! Strings can be written in single, double or triple quote

Strings!

When want to print strings, we can just use single quote or double quote. eg., print("Hello World") print('Hello World') When we don't want to interpret the escape sequences, we can prefix the string with "r". This means raw string and would not interpret escape sequences. Usage of triple quotes... using triple quotes, we can print the multi lines as follows. print("""This is multiline text. Hence, this needs to be printed as it is as multline""") Python strings cannot be changed; word="Python" word[0]="T" #Not possible Error: str object does not support item assignment String indices: Starts from 0 to length-1 Also, we can traverse from last to first using negative indices starting from -1 to -length Slicing: Strings can be sliced.. slicing is nothing but getting substring using indices eg., word[2:] -> This will print the word starting from 3rd character till the end