15 Days of Python: Day-2

Related image

Number and Strings

In this blog I am going to discuss about Numbers and Strings. First jump to numbers. 

NUMBERS:
1. Arithmetic Operations --  we can simply perform addition,multiplication, division and modulo operations on numbers.
example:  > 1+2=3
                 > 2*2 = 4
                 > 5/2 = 2.5 (float division) 
                 > 5//2 = 2 (integer division)
                 > 10%3 = 1(remainder)

2. Assignment Operation -- 
example: > width = 20
                > height = 30
                > area = width*height
                > print(area)  >> 600 

So there are nothing much to do with numbers.You can just simply perform normal mathmatical operations on it.

STRING:

1. Normal printing of strings --
>>> 'spam eggs'  # single quotes
'spam eggs'
>>> 'doesn\'t'  # use \' to escape the single quote...
"doesn't"
>>> "doesn't"  # ...or use double quotes instead
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
2. length of a string: 
example : > s = "hope is a good thing"
                >len(s)
                > 21

3. String in a way behave as a list. They follow indexing. 
example: 
s = "hope"
then s[0] = 'h'
=> s[1] = 'o'
=> s[2] = 'p' ...

4. tricks with string and list - 
example: s = "shawshank"
              > x = list(s)
              > print(x)
              > ['s', 'h', 'a', 'w', 's', 'h', 'a', 'n', 'k']
then
=> x[0] = "s" and so on....

5. Adding two strings - 
example: > s1= "shawshank" 
               > s2 = " redemption"
               > x = s1 + s2
              > print(x)
              > "shawshank redemption"

6. Reversing a string - 
example:  s = "mirror"
              >s = s[::-1]
explanation for s[::-1] -- first section represents starting index(by default 0), second section represents ending index(by default len(s)-1), and third section steps. Here -1 means, 0-1 = -1(first word of reversed string) => -1-1=-2 => -2-1 = -3 => -3-1=-4   

In the next blog will discuss about Lists in brief and many tricks that are useful in real world application.


Comments

This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post. healthcare call center software

Popular posts from this blog

Amazon Web Services

Google Code-In mentorship experience :)

Hacker Rank all java and python problem solutions