Tuesday 14 April 2020

Type Casting




Type Casting
We can convert one type value to another type. 
This conversion is called Typecasting or Type coersion. 
The following are various inbuilt functions for type casting.
1. int()
2. float()
3. complex()
4. bool()
5. str()
 
 
1.int():

We can use this function to convert values from other types to int




2. float():
 
We can use float() function to convert other type values to float type. 




 3. complex():
We can use complex() function to convert other types to complex type.
 
Form-1: complex(x) 

We can use this function to convert x into complex number with real part x and imaginary part 0.



Form2: complex(x,y): 

We can use this method to convert x and y into complex number such that x will be real part and y will be imaginary part. 






 4. bool():

We can use this function to convert other type values to bool type.





 
 
 
5. str():
We can use this method to convert other type values to str type



Pattern-7




* * * * * * * * * 
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * * 
* * 

  
Code:
n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1): 
      for i in range(1,n+2-j): 
            print("*",end=" ") 
      print()


 ******************************************************
 ******************************************************


1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6
7 7 7 7
8 8 8
9 9
10

Code:

1) n=int(input("Enter the number of rows: ")) 
2) for j in range(1,n+1): 
3)       for i in range(1,n+2-j): 
4)             print(j,end=" ") 
5)       print()

 ******************************************************
 ******************************************************


1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Code:

1) n=int(input("Enter the number of rows: ")) 
2) for j in range(1,n+1): 
3)       for i in range(1,n+2-j): 
4)             print(i,end=" ") 
5)       print()

Pattern-6


*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *

 Code - 1: 
n=int(input("Enter the number of rows:")) 
for j in range(1,n+1): 
      for i in range(1,j+1): 
            print("*",end=" ") 
      print()

Code - 2: 
n=int(input("Enter the number of rows:")) 
for i in range(1,n+1): 
      print("* "*i)

 ******************************************************
 ******************************************************


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9


n=int(input("Enter the number of rows: "))
for j in range(1,n+1): 
      for i in range(1,j+1): 
            print(i,end=" ") 
      print()

 ******************************************************
 ******************************************************



1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9

n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1): 
      for i in range(1,j+1): 
            print(j,end=" ") 
      print()




Saturday 4 April 2020

Datatypes in Python Lecture 4- bool and str




 
bool data type:

We can use this data type to represent boolean values.

The only allowed values for this data type are:
 True and False

Internally Python represents
True as 1 
and
False as 0

b=True
type(b) ------------.>bool

Example: 
 a=10
 b=20
 c=a<b
 print(c)==>True

True+True==>2
True-False==>1

 
str datatype:

str represents String data type.

A String is a sequence of characters enclosed within single quotes or double quotes.
s1='Python'
s1="Python"

By using single quotes or double quotes we cannot represent multi line string literals.

s1="Hello Learners!!
Welcome to my YouTube Channel"
For this requirement we should go for triple single quotes(''') or triple double quotes(""")
s1= '''Hello Learners!!
Welcome to my YouTube Channel '''
s1=""" Hello Learners!!
Welcome to my YouTube Channel """

We can also use triple quotes to use single quote or double quote in our String.
'''MV’s Code Guide '''
"""MV’s Code Guide"""

Indexing in Python:
In Python, Strings follows zero based index.
 The index can be either positive or negative.
Positive index means forward direction from Left to Right
Negative index means backward direction from Right to Left

>>> s="Python"
2) >>> s[0]
3) 'P'
4) >>> s[1]
5) 'y'
6) >>> s[-1]
7) 'n'
8) >>> s[40]
IndexError: string index out of range
0    1    2    3     4     5
P    y    t    h     o     n
-6  -5   -4   -3    -2    -1





Slicing of Strings:

slice means a piece
[ ] operator is called slice operator, which can be used to retrieve parts of String. 

Syntax:     variable_name[begin:end:step]

By default  begin value is 0
                  end value is length of string
                 step value is 1
If you don't specify anything then default values will be takes

Example:
 s="Python"
If you don't specify begin , end and step value
s[::]
then, internally it will be considered as
s[0:length of string:1]

See more examples below:




 Repetition operator in string:
we can use * multiplication operator in Python to perform repetition of strings
 

e.g.:  s="Python"*3
    print(s)
Output:   'PythonPythonPython'

Length of string:  

To check the length of the string we can use function len()
example:
s="Python"
print(len(s))
Output: 6

 ******************************************************
 ******************************************************




Pattern-5



J J J J J J J J J J
I I I I I I I I I I
H H H H H H H H H H
G G G G G G G G G G
F F F F F F F F F F
E E E E E E E E E E
D D D D D D D D D D
C C C C C C C C C C
B B B B B B B B B B
A A A A A A A A A A 


Code:
1
2
3
4
5
n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1):
      for i in range(1,n+1):
            print(chr(65+n-j),end=" ")
      print()



 ******************************************************
 ******************************************************

J I H G F E D C B A
J I H G F E D C B A
J I H G F E D C B A
J I H G F E D C B A
J I H G F E D C B A
J I H G F E D C B A
J I H G F E D C B A
J I H G F E D C B A
J I H G F E D C B A
J I H G F E D C B A


Code:

1
2
3
4
5
n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1): 
       for i in range(1,n+1): 
             print(chr(65+n-i),end=" ") 
       print()

Pattern-4






5 5 5 5 5 

4 4 4 4 4 
3 3 3 3 3 
2 2 2 2 2
1 1 1 1 1

Code:
1
2
3
4
5
n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1): 
      for i in range(1,n+1): 
            print(n+1-j,end=" ") 
      print()

 ******************************************************
 ******************************************************


5 4 3 2 1 
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1

 Code:
1
2
3
4
5
n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1): 
      for i in range(1,n+1): 
            print(n+1-i,end=" ") 
      print()



Pattern-3




A A A A A A A A A A
B B B B B B B B B B 
C C C C C C C C C C
D D D D D D D D D D
E E E E E E E E E E
F F F F F F F F F F
G G G G G G G G G G
H H H H H H H H H H 
I I I I I I I I I I
J J J J J J J J J J

Code:
1
2
3
4
5
n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1): 
      for i in range(1,n+1): 
            print(char(64+j),end=" ") 
      print()



 ******************************************************
 ******************************************************


A B C D E F G H I J 

A B C D E F G H I J 

A B C D E F G H I J 

A B C D E F G H I J 

A B C D E F G H I J 

A B C D E F G H I J

A B C D E F G H I J

A B C D E F G H I J

A B C D E F G H I J

A B C D E F G H I J 

Code:
1
2
3
4
5
n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1): 
      for i in range(1,n+1): 
            print(char(64+i),end=" ") 
      print()

Sunday 29 March 2020

Pattern - 2


    
1111111111
2222222222
3333333333
4444444444

5555555555
6666666666
7777777777
8888888888
9999999999

Code 1:
1
2
3
4
5
n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1): 
      for i in range(1,n+1): 
            print(j,end=" ") 
      print()

Saturday 28 March 2020

Pattern-1

  

Code 1:

1
2
3
4
5
n=int(input("Enter the number of rows: ")) 
 for j in range(1,n+1): 
       for  i in range(1,n+1):
             print("*",end='')
       print() 


Code 2:
1
2
3
n=int(input("Enter the number of rows: ")) 
for j in range(1,n+1): 
      print('*' * n)