Python Lesson 3


Lesson # 3

Hi All,


String and Multi line String

Normally we write string inside single quotes or double quotes

MyString = “Line1” or MyString = ‘Line1’

Both are correct syntax. There is no limit of string length or size. Its 1 character or might be 1000 sentences.

For the sake of readability python supports multiline string that should inside in three double quotes.

Example

MyString = “”” Line 1
Line 2
Line 3
Line 4 etc. “””

Operators

In Python there is 7 types of operators
1.       Arithmetic
2.       Assignment
3.       Comparison
4.       Logical
5.       Identity
6.       Membership
7.       Bit wise

Mostly all operators are commonly used, and you know about them.

Example

Arithmetic operators contain + - * / %
// -> Floor division
** -> Exponentiation

For more detail google it.

Python Collection (Important Topic)

So, if you are developer and doing development the most important feature of any language is collection.

It might be array, list, dictionary, tuple, set etc.
So, let’s discuss the collections in python.

There are four types of collection in python

1.       List
2.       Tuple
3.       Set
4.       Dictionary

In lesson 3, I discuss List and other collection types I discuss in next lessons.

So, let start with List

List

List is collection, which is ordered and changeable, it allows duplicate members. By duplicate members means if you add ‘Salman’ in list you can add more ‘Salman’ in other order.

Example

Order 1 -> “Salman”
Order 2 -> “Salman”

Syntax of List

The list is enclosed with [ ] brackets and values are separated with comma ,

Example

MyList = [“Salman”, “Salman”, “Mushtaq”, “60”]

And to access the items in list you need to pass order number also called index that starts from 0

print(Mylist[0]) -> “Salman”
print(Mylist[1]) -> “Salman”
print(Mylist[2]) -> “Mushtaq”
print(Mylist[3]) -> “60”

You can also access the list with negative index

Example

print(MyList[-1]) -> “60”

The negative index starts from -1 and if you try to print with -0, python interpreter considers this index as 0

You can also access the list with range

Example

print(MyList[1:3]) -> [‘Salman’,’Mushtaq’,’60’]

In range there is 2 values and if you don’t know the length you can use the len method of string to find out the length otherwise keep it blank and python interpreter automatically get the length of list
To find out the length of string or list use len method

print(len(MyList))   -> 4

You can change the value

MyList[2] = “new Value”

Loop and List

You can go through the list one by one with the help of for loop

MyList = [‘Value1’,’ Value2’,’ Value3’,’ Value4’,’ Value5’]
For x in MyList:
    Print(x)
Output:
Value1
Value2
Value3
Value4
Value5

Later you can add any conditions to find out the expected result.

Add Items in Existing List

You can use append method to add the items in existing list

MyList.append = (“New Value 6”)

If you need to add the item in specific index you can use insert method

MyList.insert(index, value)
MyList.insert(5, “Another new value 7”)

Similarly, you can remove the specific items by using remove method

MyList.remove(“New Value 6”)

Use pop method to remove the last record

Use del method to delete the specific value by index

del MyList[2]

use clear method to make the list empty

MyList.clear()

Use copy method to copy the list

NewList = MyList.copy()

Aother method to copy the list is to use the keyword list

NewList = list(MyList)

You can use + symbol to join the 2 list

ThirdList = MyList + NewList

See you in lesson 4 :-)


Comments

Popular posts from this blog

Python Lesson 4

Lesson 6 - Loops (While), Break, Continue