Tuesday, June 30, 2020

Inner Life of the Lists


See the below code lines.








Output is





Slices in Lists
A slice is an element of Python syntax that allows you to make a brand new copy of a list, or parts of a listIt actually copies the list's contents, not the list's name.







Output is




This inconspicuous part of the code described as [ : ] is able to produce a brand new list.


General form of the slice is given below.

myList [start : end]

  • start is the index of the first element included in the slice.
  • end is the index of the first element not included in the slice.


A slice of this form makes a new (target) list, taking elements from the source list - the elements of the indices from start to “end-1”.

Here not to “end” but to “end-1”. An element with an index equal to “end” is the first element which does not take part in the slicing.








The “newList” list will have “end – start” (3 - 1 = 2) elements - the ones with indices equal to 1 and 2 (but not 3).



Output is



The Bubble Sort


The program given below using the “while” loop.













Output is




The Bubble Sort – Interactive Version

Python, however, has its own sorting mechanisms. No one needs to write their own sorts, as there is a sufficient number of ready-to-use tools.

See the below examples.

Example -1






Output is



All the lists have a method named “sort ()” which sorts them as fast as possible.

Example -2


















Output is














reverse () Method

Example -1




Output is






Example -2











Output is


Lists in action


Example -1








Example -2



The output is same in both above examples.





Example -3




























Output is




The lists can be nested.

E.g.- myList = [1, ’a’, [“list”, 64, [0, 1], False]]

Sunday, June 14, 2020

Function vs. Methods for Lists


A method is a specific kind of function. It behaves like a function & looks like a function, but differs in the way in which it acts and in its invocation style.

Does all the things, but is also able to change the internal state of a selected entity.

Owned by the data it works for, while a function is owned by the whole code.

result = data.method(arg)

The name of the method is preceded by the name of the data which owns the method. Next we add a dot, followed by the method name, and a pair of parenthesis enclosing the arguments.

A function doesn’t belong to any data. Actually it gets data, it may create new data and it generally produces a result.

It takes an argument, does something and returns a result.

result = function(arg)


Adding elements to a list

append () Method

It takes its argument’s value & puts it at the end of the list which owns the method. The list’s length then increases by one.

list.append(value)


insert () Method

It can add a new element at any place in the list, not only at the end.

list.insert(location, value)



See the below examples.

















When by creating empty lists,

Example -1








Output is






Example -2








Output is






For Loop with Lists


Example -1










Example -2









The output is 27 in both above examples.

Thursday, June 11, 2020

More about Lists.....


The list starts with an open square bracket and ends with a closed square bracket.

The elements inside a list may have different types. Some of them may be integers, others floats and yet others may be lists.

Python has adopted a convention stating that the elements in a list are always numbered starting from zero. This means that the item stored at the beginning of the list will have the number zero.


Indexing Lists

The value inside the brackets which selects one element of the list is called an index, while the operation of selecting an element from the list is known as indexing.









And now we want the value of the fifth element to be copied to the second element.






Accessing list content





The len( ) function


Removing elements from a list
Here we used the keyword “del”. It’s an instruction, not a function.







We can't access an element which doesn't exist - we can neither get its value nor assign it a value. Both of these instructions will cause runtime errors now.












Negative Indices are legal
In lists, negative indices are also legal. See the below example.












Next Example,












Output is



Monday, June 8, 2020

Binary Left Shift & Binary Right Shift


Shifting is applied only to integer values and mustn’t use float values.

Shifting a value one bit to the left thus corresponds to multiplying it by two. Shifting one bit to the right is like dividing by two.

Shift operators in Python are a pair of diagraphs << and >>


The left argument of these operators is an integer value whose bits are shifted. The right argument determines the size of the shift.








Given below the updated priority table.



Given below some of the examples.


















Bitwise Operators


This is the “xor” (exclusive or) operator and is denoted as ^ (caret). Given below all the bitwise operators.

& (ampersand) – bitwise conjunction

| (bar) – bitwise disjunction

~ (tilde) – bitwise negation

^ (caret) – bitwise exclusive (xor)



Given below the truth table.



The arguments of these operators must be integers. We must not use floats here.


The logical operators don’t penetrate into the bit level of its argument. They’re only interested in the final integer value.


These operators can be used in abbreviated form.









Wednesday, June 3, 2020

Logical Operators


and operator


It’s a binary operator with a priority that is lower than the one expressed by the comparison operators. It allows us to code complex conditions without the use of parentheses like this one.


Counter > 0 and value == 100


The truth table is given below.


or operator


This is a disjunction operator. It’s a binary operator with a lower priority than “and”.

The truth table is given below.




not operator


This is a unary operator performing a logical negation. It turns truth into falsehood and falsehood into truth.



Logical Expressions


See the below code lines & outputs.











In Python,

not (p and q) == (not p) or (not q)
not (p or q) == (not p) and (not q)

Break & Continue Statements

break - exits the loop immediately, and unconditionally ends the loop's operation, the program begins to execute the nearest instruction after the loop's body.

continue - behaves as if the program has suddenly reached the end of the body, the next turn is started and the condition expression is tested immediately.


Example 1 –











Output is










Example 2 –











Output is













Example 3 –





















Output is















Example 4 –























Output is
















Example 5 –



















Output is













The loop's "else" branch is always executed once, regardless of whether the loop has entered its body or not

If the control variable doesn't exist before the loop starts, it won't exist when the execution reaches the "else" branch.