Thursday, October 17, 2019

Learning Python -Day 4


DAY 4

n=int(input("Enter a number"))
i=
1
while(i<=n):
   
print(end=" ""hello")
    i+=
1
run the function CTL+shift+fn+F10
Enter a number15
 hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello
WHILE LOOP IS USED WHEN WE KNOW THE CONDITION, THE LOOP WILL LOOK INTO
Factorial example
n=int(input("Enter a number"))
i=n
f=
1
while(i>=1):
    f=f*i
    i-=
1
   
print("fatorial of",n,"is",f)
run the function CTL+shift+fn+F10

Enter a number7
fatorial of 7 is 7
fatorial of 7 is 42
fatorial of 7 is 210
fatorial of 7 is 840
fatorial of 7 is 2520
fatorial of 7 is 5040
fatorial of 7 is 5040

Find Sum of digits example
n=int(input("Enter a number"))
s=
0

while(n!=0):
    r=n%
10
   
n=n//10
   
s=s+r
print("sum of digits ",s)
    print("not a palindrome")
run the function CTL+shift+fn+F10
Enter a number435
sum of digits  12



Reverse of a number
n=int(input("Enter a number"))
m=n
a=
0

while(n!=0):
    r=n%
10
   
n=n//10
   
a=a*10+r
print("Reverse number is ",a)
if(a==m):
   
print("palindrom no")
else:
   
print("not a palindrome")
run the function CTL+shift+fn+F10
Enter a number131
Reverse number is  131
palindrom no

STRING

str1="This is Python string"
print("String is",str1)
run the function CTL+shift+fn+F10
String is This is Python string
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
T
H
i
s

I
s

P
Y
T
H
O
N

S
T
R
I
N
g

str1="This is Python string"
print("str1[2]",str1[2])
run the function CTL+shift+fn+F10

The output is I as string 2 is i
 also

Indexing

It is helpful to think of a string as an ordered sequence. Each element in the sequence can be accessed using an index represented by the array of numbers:
The first index can be accessed as follows:
Because indexing starts at 0, it means the first index is on the index 0. 
To find length of the string



str1="This is Python string"

print("Length of string is",len(str1))

run the function CTL+shift+fn+F10
Length of string is 21


str1="This is Python string"

print(str1.index("i"))
run the function CTL+shift+fn+F10
2
str1="This is Python string"

print(str1[3:11])
run the function CTL+shift+fn+F10
s is Pyt
It will end on 10 instead of 11 as its value is n-1
 
str1="This is Python string"

print(str1[2:15:2])
run the function CTL+shift+fn+F10
i sPto
 
Here in the above code, [2:15:2]), first 2 is the start, 15 is the end and 2 is the step or gap of
 
program for the reverse string print
 
 
str1="This is Python string"

print(str1[::-1])
run the function CTL+shift+fn+F10
gnirts nohtyP si sihT
 
 
 
If you need to find index within range
 
str1="This is Python string"

print(str1.index("i",3,10))
run the function CTL+shift+fn+F10
5
To count no. of times the letter repeats itself in the string


str1="This is Python string"

print("Count number of i in string",str1.count("i"))
run the function CTL+shift+fn+F10
Count number of i in string 3

To convert string in upper case


str1="This is Python string"

print("String in upper case is ",str1.upper())
 
run the function CTL+shift+fn+F10
String in upper case is  THIS IS PYTHON STRING
 
To convert string in lower case
 
str1="This is Python string"

print("String in lower case is ",str1.lower())
run the function CTL+shift+fn+F10
String in lower case is  this is python string
 
To make first letter as Capital
 
str1="This is Python string"

print("String capitalize ",str1.capitalize())
run the function CTL+shift+fn+F10
String capitalize  This is python string

To make first letter of every word capital


str1="This is Python string"

print("String in title case is ",str1.title())
run the function CTL+shift+fn+F10
String in title case is  This Is Python String

To check if string is in upper case, lower case or title or alphabets are contained in string only or alphanumeric string

print("Srting in upper case??",str1.isupper())
print("Srting in lower case??",str1.islower())
print("Srting in title case??",str1.istitle())

print(str1.isalpha())

print(str1.isalnum())



str1="This is Python string"

print(str1.startswith("Thi"))
run the function CTL+shift+fn+F10
True

These all are check commands


print(str1.startswith("thi"))

print(str1.endswith("ython"))

print(str1.endswith("tring"))





s1='Ptyhon'

for i in s1:

    print(i)





print(s1*3)

print(s1+"Hello")



s1='Ptyhon'

print(s1.center(10))
run the function CTL+shift+fn+F10
  Ptyhon  

s1='Ptyhon'

print(s1.center(10,"$"))

run the function CTL+shift+fn+F10
$$Ptyhon$$
To print from left


print(s1.ljust(10))
To print from left and fill the spaces


s1='Ptyhon'

print(s1.ljust(10,'*'))
run the function CTL+shift+fn+F10
Ptyhon****



To print from right


s1='Ptyhon'

print(s1.rjust(10,'#'))

run the function CTL+shift+fn+F10
####Ptyhon



To find index from the right

str1="This is Python string"

print(str1.rfind("i"))
run the function CTL+shift+fn+F10
18

Learning Python- Day 5

No comments:

Post a Comment

Featured Post

Ichimoku cloud

Here how you read a ichimoku cloud 1) Blue Converse line: It measures short term trend. it also shows minor support or resistance. Its ve...