Monday, December 23, 2019

Chapter -10 (Mixed Question)


For learn how to download and install python link here: How to download and install python in window


For learn how to run and execute the code in python link here:How to write and run the code


In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.



Example-1: Write a program and make a list.




Code:

x=list(range(1,4)

print(x)


Result:
[1, 2, 3]

Explanation:- You read in previous chapter 6.

Example-2: Write a program and make a list.


Code:
x=list('abc')
print(x)

Result:
['a', 'b', 'c']

Explanation:- You read in previous chapter 6.

Example-3: Some important expression


Example-4: Write a program that write all the palindromic numbers between 1 and 1000. A palindromic number is one that is the same backwards as forwards, like 121.


Code:
for i in range(1,10001):
    s = str(i)
    if s==s[::-1]:
        print(s)

Result:
1
2
3
4
......

Explanation:- In the 3rd line you see s==s[::-1] means if the number is equal to reverse as well as forward. You see this command in previous chapter 7


Example-5: Write a program which tells a person born on January 1, 1991 how old they are in 2010.


Code:
birthday = 'January 1, 1991'
year = int(birthday[-4:])
print('You are', 2010-year, 'years old.')

Result:
You are 19 years old.

Explanation:- In the 2nd line you see int command means first we make it integer and then use [-4:] it means last four number of the birthday date i.e., 1991 and then we minus from 2010.


Example-6.1: Write a program that takes a number num and adds its digits. For instance, given the number 38, the program should return 11 (which is 3 + 8). Let us start with a 2-digit example.


Code:
num=38
digit = str(num)
answer = int(digit[0]) + int(digit[1])
print(answer)

Result:
11

Explanation:- First we make the num command into string and then we use digit[0] means first number i.e.3 and then digit[1] 2nd number i.e.8 means we add 3+8=11


Example-6.2: Write a program that takes a number num and adds its digits. For instance, given the number 38, the program should return 11 (which is 3 + 8). Let us start with a 2-digit example.


Code:
num=38
digit = str(num)
answer = 0
for i in range(len(digit)):
    answer = answer + int(digit[i])
print(answer)

Result:
11

Explanation:- We solve the previous example by alternative method.In the 5th line we see int(digit[i]) means it add the first number and then 2nd number and then add them.

Example-6.3: Write a program that takes a number num and adds its digits. For instance, given the number 38, the program should return 11 (which is 3 + 8). Let us start with a 2-digit example.


Code:
num=38
answer = sum([int(c) for c in str(num)])
print(answer)

Result:
11

Explanation:- This is the alternative method of solve the problem. By this we can solve the problem in one code.You read all these in previous chapter-7,8.

Example-7: Write a program like 38.63-38=.63


Code:
num=38.63
b= int(num)
a=num - int(num)
print(a)

Result:
0.6300000000000026

Explanation:- In the 2nd line we use int, function of int is make the integer means if it contain number after decimal then it only include the number before decimal i.e., 38.

Example-8: To find number which give the perfect square root in the range of 100.

Code:
num=100
for i in range(2,int(num**.5)+1):
    print(i)

Result:
2
3
4
5
6
7
8
9
10

Explanation:- We use int means it make it integer and then num**.5, 0.5 means it make square root and also use +1 , because we know range not include the last number.

Example-9.1: Error showing when you try to find alphabet in a word.

Code:
words='becoz'
for w in words:
    if w[4]=='z':
        print(words)

Result:
String index out of error.


Example-9.2: Write a program and if at the 5th position of the word if z found then print 'z found'.


Code:
w='becoz'
if len(w)>=5 and w[4]=='z':
    print('z found')

Result:
z found

Explanation:- In previous example it show error because string length may be more than 5 in this case so show error in previous case. But now we add if len(w) not show error.

Example-10: If extra space not present and you try to continue the code use backlash.


Code:
if 'a' in string or 'b' in string or 'c' in string \
or 'd' in string or 'e' in string:

Explanation:- If we not use backlash then it show error.

Example-11.1: Write a program and give tip of 25% the user paid 23.60 dollar.


Code:
a = 23.60 * .25
print('The tip is {:.2f}'.format(a))

Result:
The tip is 5.90

Explanation:- In 2nd line we see{:.2f} then the answer with 2 decimal.

Example-11.2:  Write a program and give tip of 25% the user paid 23.60 dollar.


Code:
a = 23.60 * .25
print('The tip is {:.1f}'.format(a))

Result:
The tip is 5.9

Explanation:- In 2nd line we see{:.1f} then the answer with 1 decimal.

Example-11.3:  Write a program and give tip of 25% and bill with total amount.


Code:
bill=23.60
tip = 23.60 * .25
print('Tip: ${:.2f}', Total: ${:.2f}'.format(tip, bill+tip))

Result:
Tip: $5.90, Total:$29.50

Explanation:- In 3rd line you see {:.2f}, , first write here tip then total so,tip for tip and total for bill+tip. 

Example-11.4: Write a program and print letter starting from right side.


Code:
print('{:3d}'.format(2))
print('{:3d}'.format(25))
print('{:3d}'.format(138))

Result:
    2
  25
138

Explanation:- Here in result you see 2 is at the most right side, then 25 is on right side also but 138 is attach to the margin of left side. Because in format you write{:.3d} means three space. If you write here 4 then you see other result.

Example-11.5: Write a program and print letter in centre.


Code:
print('{:^5d}'.format(2))
print('{:^5d}'.format(222))
print('{:^5d}'.format(13834))

Result:
   2
 222
13834

Explanation:- Here in code you write {:^5d}, in previous you use {:.3d} dot(.) before 3 it means for right side. But in this we use "^" this symbol which denote number print in the centre and also here use 5 it means within the 5 space.


Example-11.6: Write a program and print letter in centre.


Code:
print('{:^10s}'.format('Hi'))
print('{:^10s}'.format('there!'))

print('\n')

print('{:^8s}'.format('Hi'))
print('{:^8s}'.format('there!'))

print('\n')

print('{:^5s}'.format('Hi'))
print('{:^5s}'.format('there!'))

Result:
    Hi    
  there!  


   Hi   
 there! 


 Hi  
there!

Explanation:- Here we use ^s but not ^d because d is use for number but s is used for alphabet but here you use quote like 'Hi' so 'hi' should be in quotes. You can also use number here but number also put in quote. print('\n') use to give gap in the code.

Example-11.7: Write a program and print letter in right side.


Code:
print('{:>6s}'.format('Hi'))
print('{:>6s}'.format('There'))

Result:
      Hi
 There

Explanation:- This is alternative method to solve the example-10.4. In this example we solve the problem by using '>s' , Here greater than(>) is used to print the number in right side.

Example-11.8: Write a program and print letter in left side.


Code:
print('{:<6s}'.format('Hi'))
print('{:<6s}'.format('There'))

Result:
Hi
There

Explanation:- Here we use '<s' , lesser than '<' sign is use to print number in left side.

Example-12: Print a 10 *10 multiplication table.


Code:
for i in range(1,11):
    for j in range(1,11):
print('{:3d}'.format(i*j), end=' ')
    print()

Result:
  1   2   3   4   5   6   7   8   9  10 
  2   4   6   8  10  12  14  16  18  20 
  3   6   9  12  15  18  21  24  27  30 
  4   8  12  16  20  24  28  32  36  40 
  5  10  15  20  25  30  35  40  45  50 
  6  12  18  24  30  36  42  48  54  60 
  7  14  21  28  35  42  49  56  63  70 
  8  16  24  32  40  48  56  64  72  80 
  9  18  27  36  45  54  63  72  81  90 
 10  20  30  40  50  60  70  80  90 100

Explanation:- Here we use for loop and then again loop which is under for loop. In the first line it show row and in 2nd line it show column and in 3rd we multiply then then it print the table.

Example-13: Write a program and  find all the integer solutions (x, y) to the system 2x + 3y = 4, x- y = 7, where x and y are both between -50 and 50.


Code:
for x in range(-50,51):
    for y in range(-50,51):
        if 2*x+3*y==4 and x-y==7:
            print(x,y)

Result:
5 -2

Explanation:- It is simple first we use loop for range and then put the condition to solve the problem.

Example-14.1: Pythagorean triple is a triple of numbers (x, y, z) such that x**2+ y**2 = z**2. For instance (3, 4, 5) is a Pythagorean triple because 3**2 + 4**2 = 5**2. Pythagorean triples correspond to triangles whose sides are all whole numbers (like a 3-4-5-triangle). Here is a program that finds all the Pythagorean triples (x, y, z) where x, y, and z are positive and less than 100.


Code:
for x in range(1,100):
    for y in range(1,100):
        for z in range(1,100):
            if x**2+y**2==z**2:
                print(x,y,z)

Result:
3 4 5
4 3 5
5 12 13
6 8 10
7 24 25
8 6 10
8 15 17
9 12 15
9 40 41
10 24 26
11 60 61
12 5 13
12 9 15
12 16 20
12 35 37
13 84 85
14 48 50
15 8 17
15 20 25
...............

Explanation:- You see these equation in mathematics in the 9th standard. We know 3 unknown value in this example i.e., x,y,z and these value within the 100 so use loop which help in find those number and then we put here condition in the 4th line if x**2+y**2=z**2 and then it solve the problem which satisfy the equation.

Example-14.2: In the previous example we found the triple by follow that equation but Pythagorean triples, those that aren’t multiples of another triple. The way it does this is every time a new triple is found, it checks to make sure that x, y, and z are not all
divisible by the same number.


Code:
for x in range(1,100):
    for y in range(1,100):
        for z in range(1,100):
            if x**2+y**2==z**2:
                for i in range(2,x):
                    if x%i==0 and y%i==0 and z%i==0:
                        break
                else:
                    print((x,y,z), end='')

Result:
(3, 4, 5)(4, 3, 5)(5, 12, 13)(7, 24, 25)(8, 15, 17)(9, 40, 41)(11, 60, 61)(12, 5, 13)(12, 35, 37)(13, 84, 85)(15, 8, 17)(16, 63, 65)(20, 21, 29)(21, 20, 29)(24, 7, 25)(28, 45, 53)(33, 56, 65)(35, 12, 37)(36, 77, 85)(39, 80, 89)(40, 9, 41)(45, 28, 53)(48, 55, 73)(55, 48, 73)(56, 33, 65)(60, 11, 61)(63, 16, 65)(65, 72, 97)(72, 65, 97)(77, 36, 85)(80, 39, 89)(84, 13, 85)

Explanation:- Here we use some more code to find the number are not multiple of each other. To find the number are not multiple we use x%i==0 and also y and then z. It means it start divide the x from 1 to 100 and if completely divisible then it divide y and z, If a single number divide completely then the loop break it not print that triple.

<For example , in the Example-13.1 we got result(6,8,10) and we know it completely divide by 2. So this number is not Pythagorean triples and not found in result of Example-13.2.>


Practice Question:


No comments:

Post a Comment

If you have any doubt, let me know.

How and Why you should learn python? About Python Programming Language Python is the programming language. By the help of Python you can ...