Wednesday, May 23, 2018

python chap 5(Hi friends this is the continuation of previous post so i kindly request you to read that before this post. As this blog is also prepared for beginners so i advice you that you don't need to by-heart this lessons just read and understand it if you have any doubt ask in the comment section )

STRING

The built in string object is a sequence of character used to store and represent text based information (plain strings are also some times used to store and represent arbitrary sequences of binary bytes ).a string can be pair of single quoted or double quoted. Subsets of strings can be taken using the slice operater( [], [:] ) with indexes starting at 0 in the beginning of the string and working there way from -1 to the end strings.Strings are immutable
my_values = 'I\'m nadeem and this are my\
                  string object for the single quote' 
my_values1 = "I'm nadeem and this are my\
                 string object for the double quote"  
my_values2 = """I'm nadeem and this are my
                  string object for the triple quote"""
print(my_values)            # prints the whole string
print(my_values[0])        #prints the first character
print(my_values[2:5])    # prints character from third to five
print (my_values [2:])    #print the characters starting from the third to the end


In the above example you have seen the backslashes on the single quoted value which on printing the output will display only the single quote in between the I and m or you can use like in the second value were a pair of double quote is used. There the single quote is placed  between the I and m here backslash is not needed and in the single quote and double quote string here the line break is defined by using backslashes. And we can use triple quoted strings so that to break line we doesn't need backslashes line break is defined as we have defined in the string literal. And strings can be indexed and sliced in the given example with print() statement above were the first one prints the whole value and the second one prints the first character and the third one will print the characters from third to the fifth one and the fourth one will print the character from the third to the end character.String also supports concatenation and here are some examples given below

my_value ="My name is  "
my_second = my_value + "nadeem"
print(my_second)

the value here are concatenated and the result is as given here
My name is nadeem
Okay guys more will be discussed on string in the coming lessons

LISTS

The lists is a mutable sequence of items the items inside the lists are arbitrary objects and may be of different types like strings and numbers The list defined using a square bracket were the items are put with commas in between the items and optionally we can add a comma after the last item. To denote empty list define a empty square bracket examples are given below

nads_sad = []             #empty list
nads_sad = ['animals','birds',24,26,24.35]  #list with different data types

In the above example we saw the empty list and the list with values of different types .Like strings (and like all other builtin sequence type) lists can be indexed and sliced All slice operations returns a new list containing the requested elements .Lists also supports operations like concatenation and Unlike strings which are immutable lists are mutable it is possible to change their content you can also add items at the end of the list the built in function len() also applies to list which gets the length of the list it is also possible to nest lists can create list containing other lists here are some examples for this 
first we will see list indexed and sliced

my_list = ['a','b','c','d','e','f']
print(my_list[0])    #prints the first element in the list
print (my_list[2:])  #prints items from third to the end
#list concatenation
my_list2 = ['h','j','p']
print(my_list + my_list2) # outputs #['a','b','c','d','e','f','h','j','p']
#list append() statement adds element to the end of the list
my_list .append('x') #prints the value ['a','b','c','d','e','f','x']
#now applying the built-in function len()
len(my_list)
#prints 6
#nested list creating list containing another list
my_list3 = ['a','b','c']
my_list4 = [2,4,6]
a = [my_list3,my_list4]
# if a is printed it will out put as [['a','b','c'],[2,4,6]]



Thats all for this post friends more lessons will be on the next post 

THANKS FOR VIEWING MY BLOG HOPE YOU LIKE THE POST IF YOU WANT TO GET LATEST POSTS ON THIS PLEASE FOLLOW BY EMAIL ID AND FEEL FREE TO ASK YOUR DOUBTS AND DON'T FORGET TO SHARE,COMMENT AND FOLLOW


No comments:

Post a Comment