Tuesday, October 15, 2019

Learning Python- Day 3


DAY 3
             
Today we will be doing Loops. There are 2 types of loops 1) for 2) while.
Todays focus is for loop
i in the for loop is a variable with values between 0 and n-1
n=int(input("Enter a number"))
for i in range(n):
   
print("hello")
run the function CTL+shift+fn+F10

Enter a number10
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello


n=int(input("Enter a number"))
for i in range(n):
   
print(i)
run the function CTL+shift+fn+F10
Enter a number7
0
1
2
3
4
5
6
n=int(input("Enter a number"))
for i in range(1,n+1):
   
print(i)
run the function CTL+shift+fn+F10
Enter a number7
1
2
3
4
5
6
7
n=int(input("Enter a number"))
for i in range(1,n+1,2):
   
print(i)
run the function CTL+shift+fn+F10
Enter a number7
1
3
5
7
If we look at the above program,and look at the line for i in range(1,n+1,2): Here 1 is start, n+1 is the end and 2 is the step
Table example
n=int(input("enter a number"))

print("table of",n,"is")

for i in range(1,11):

    print(n*I, end=" ")
run the function CTL+shift+fn+F10
enter a number7
table of 7 is
7 14 21 28 35 42 49 56 63 70
Use end=" " to have output in same line or row

Even number example
n=int(input("enter a number"))
print("even no. from 1 to",n,"are")
for i in range(1,n+1):
   
if(i%2==0):
       
print(i)
run the function CTL+shift+fn+F10
enter a number7
even no. from 1 to 7 are
2
4
6
Even numbers within a range
l=int(input("enter a lower range number"))

u=int(input("enter a upper range number"))

print("even no. from ",l,"to",u,"are")

for i in range(l,u+1):

    if(i%2==0):

        print(I,end=" ")
run the function CTL+shift+fn+F10
enter a lower range number5
enter a upper range number15
even no. from  5 to 15 are
6 8 10 12 14
Sum of natural numbers
n=int(input("enter a number"))
s=
0
for i in range(1,n+1):
    s=s+i
   
print("sum=",s,end=" ")
run the function CTL+shift+fn+F10
enter a number5
sum= 1 sum= 3 sum= 6 sum= 10 sum= 15

Whether given no is prime or  not example

n=int(input("enter a number"))
f=
0
for i in range(2,n):
   
if(n%i==0):
        f=
1
       
break
if
(f==0):
   
print("prime no")
else:
   
print("not a prime no")
run the function CTL+shift+fn+F10
p
enter a number7
prime no
Prime no. within range
l=int(input("enter a lower range number"))
u=
int(input("enter a upper range number"))
print("Prime no. from",l,"to",u,"are")
for i in range(l,u+1):
    f=
0
for j in range(2,i//2+1):
   
if(i%j==0):
        f=
1
       
break
if
(f==0):
   
print(i)

Backward loop
n=int(input("Enter a number"))
for i in range(n,0,-1):
   
print(i,end=",")
run the function CTL+shift+fn+F10
Enter a number9
9,8,7,6,5,4,3,2,1,

Learning python- Day 4

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...