Saturday, December 14, 2019

Fibonacci series


# Fibonacci series:
# the sum of two elements defines the next
a, b = 0, 1 #Multiple Assignment
while a < 10:  #While loop to be executed till value of a less then 10
     print(a)
     a, b = b, a+b  #Multiple Assignment 2

Let’s take a look at the above program:

The first line demonstrates multiple assignment where we are assigning value to two variables a and b at the same time with values 0 and 1 respectively. On the last line I have again used multiple assignments, to establish the fact that the expressions on the right-hand side are all first evaluated and then the assignment will take place. Here I shall remind you that the expressions are always evaluated from the left to the right irrespective of the side of expression.

The while loop (also considered as entry control loop as it checks for the validity of the expression based on the condition specified therefore it) executes as long as the condition here: a < 10 is evaluated to true. Python consider any non-zero integer value as true while zero is treated as false.

Strings:- Text Sequence Type :- str


In Python, strings are immutable sequence of Unicode code points. One can write string literals in any of the following ways:
  • Single quotes: 'allows embedded "double" quotes'
  • Double quotes: "allows embedded 'single' quotes".
  • Triple quoted: '''Triple quoted strings may extent to multiple lines’’’ “””- all associated white-space or blank space will be included in the string literal”””

Important Note: If there are string literals with only whitespace between them and are a part of any single expression than they will be implicitly converted to a single string literal. See the following example code piece.

>>> "Meetu " "Choudhary"
'Meetu Choudhary'
>>> print ("Meetu " "Choudhary" )
Meetu Choudhary
>>>

Sunday, December 1, 2019

Using Python Shell for Working with Strings: Part -II



To read previous part of using Python Shell for working with strings: Part -1 click here

By default strings are indexed. Indexing starts with the first character as index 0.  Note: in Python there is no separate data type as character, a character is simply a string of size one

In Python indices can also be negative numbers, negative number indices are used to start counting from the right of the string. Take a look at Example at 5

Note that since -0 is the same as 0, negative indices start from -1.

Example 5:
>>> m='Meetu'
>>> m[0]  #first character of variable m
'M'
>>> m[4] #Last Character of variable m
'u'
>>> m[-1] #Last character of variable m using negative indices
'u'
>>> m[-5] #First character of variable m using negative indices
'M'
>>>
With indexing Python also supports slicing. This means extracting more than one character form the strings also called as substring.

Example 6:
>>> m= 'Meetu Choudhary' #Setting Variable m
>>> m[0:4] #Slicing [Extracting first 4 Characters]
'Meet'
>>> m[6:15] #Slicing [Extracting characters from middle of the string till end]
'Choudhary'
>>>#Above two statements using print() function
>>> print(m[0:4])
Meet
>>> print(m[6:15])
Choudhary
>>> m='Gaurav Nanda' #resetting variable m value
>>> print(m[8:11]) #Slicing [Extracting characters from middle of the string to the specified no of characters by defining last +1 character index ] using print() function
and
>>> m[8:11] # above Example without print() function
'and'
>>>

Important Learning:

See in the above example how the starting index is always included, and the end index is always excluded. This is to makes sure that s[:i] + s[i:] is always equal to s:


Interesting Facts:

Python slice indices can be used with its useful default values; an omitted or null first index implies its default value which is zero, an omitted or null second index has its default value to the size of the string being sliced. See Example No 7

Example 7:

>>> m='Gaurav Nanda' #Setting Variable Value
>>> m[:6] # Omitted start index
'Gaurav'
>>> m[7:] #Omitted last index
'Nanda'
# Same example using print() function
>>> print(m[:6])
Gaurav
>>> print(m[7:])
Nanda
>>>

Note:


To check the length of the string we can use a Build-in function i.e. len See Example no 8.

Example 8:

>>> m="Gaurav Nanda" # Setting Variable value
>>> len(m) # Using build in function len
>>>

Using Python Shell for Working with Strings: Part -1


In previous article we have learnt about how to use Python Shell as a simple calculator using numbers. In this article we are going to learn how we can use Python Shell to work with Strings, as it helps us to express Strings in many ways.

Points to ponder:

1. Strings can be enclosed in single quote ('.......') or double quote (".......").
2. Back Slash \ can be used to avoid or escape the quote symbols.

Example 1:
>>> 'Hello World' # using single quotes
'Hello World'
>>> "Hello World!" # using double quotes
'Hello World!'
>>> 'Meetu's'
SyntaxError: invalid syntax
>>> 'Meetu\'s'  # use \' to escape the single quote...
"Meetu's"
>>> "Meetu's" # ...or use double quotes instead
"Meetu's"
>>> '"Yes", She Said'
'"Yes", She Said'
>>> 

Sometimes working directly with Single Quotes, Double Quotes and using Back Slash for more formatted output seems tiresome and clumsy. Which leads to many formatting errors and harder to recognize them, also decreasing its readability. Here comes the Print Function to our rescue. The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters see example 2


Example 2:
>>> '"Meetu\'s," Blog' # use \' to escape the single quote...
'"Meetu\'s," Blog'
>>> print ('"Meetu\'s," Blog') # use \' to escape the single quote...
"Meetu's," Blog
>>> myString = 'First line.\nSecond line.' # \n means newline
>>> myString  # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(myString) # with print(), \n produces a new line
First line.
Second line.
>>>


Sometimes we would prefer that special characters starting with \ should not be interpreted as special characters rather they must be treated as simple strings. This task can be achieved by a very simple method you just need to place a character r before placing the first quote in string. This r stands for raw strings  

Example 3:
>>> print('C:\Python\name') # here \n means newline!
C:\Python
ame
>>> print(r'C:\Python\name') # note the r before the quote
C:\Python\name
>>>

Strings concatenation (adding two or more strings)  can be done by using + operator, also we can repeat a string for a specified no of times using * operator take a loko on following Example no 4

Example 4:

>>> 'Meetu' + ' ' + 'Choudhary'  # Concatenation using + operator  
'Meetu Choudhary'
>>> print('Meetu' + ' ' + 'Choudhary') # Concatenation using + operator in print () function 
Meetu Choudhary

>>> 2 * 'Meetu Choudhary' # Multiplication using * operator 
'Meetu ChoudharyMeetu Choudhary'
>>> print (2 * 'Meetu Choudhary') # Multiplication using * operator  using print() function

Meetu ChoudharyMeetu Choudhary
>>> m='Meetu' # a String Variable

>>> m + ' Choudhary' #Concatenation using Variable

'Meetu Choudhary'

>>> print (m + ' Choudhary') #Concatenation using Print and Variable

Meetu Choudhary
>>> print (2 * m)  #Multiplication using print() function and variable

MeetuMeetu
>>> 2*m  #Multiplication using variable

'MeetuMeetu'
>>>


Working with strings will be continued in Part II visit here