## 16 Examples of Python + Codes

2013/02/21 Good I leave 16 examples here them or simple exercises in the Phyton language with their Codes remember is not to copy is to practice mainly to understand well that it treats the exercises and their codes. :)

1. Program that prints the 25 first natural numbers

```###Programa that prints the 25 first natural numbers n = 1 while n <= 25:
print n,
n += 1```

2. To print the odd numbers from the 1 to the 25, both inclusively

```to ###Imprimir the odd numbers from the 1 to the 25, both inclusively n = 1 h = '' while n <= 25:
if n%2! = 0:
h += ‘%i’ % n
n += 1 print h```

3. To print the even numbers from the 40 to the 60, both inclusively

```to ###Imprimir the even numbers from the 40 to the 60, both inclusively n = 40 h = '' while n <= 60:
if n%2 == 0:
h += ‘%i’ % n
n += 1 print h```

4. To print numbers 48, 52, 56,…, 120

```to ###Imprimir numbers 48, 52, 56,…, 120 n = 48 h = '' while n <= 120:
h += ‘%i’ % n
n += 4 print h```

5. To calculate and to print sum 1+2+3+4+5+… +50

```to ###Calcular and to print sum 1+2+3+4+5+… +50 n = 100 h = '' while n >= 20:
h += ‘%i’ % n
n - = 5 print h```

6. To calculate and to print sum 1+2+3+4+5+… +50

`to ###Calcular and to print sum 1+2+3+4+5+… +50 h = range (1, 51) print sum (h) #con the commando sum adds the numbers of a list`

7. To calculate and to print the product 1*2*3*4*5*… *20

```to ###Calcular and to print the product 1*2*3*4*5*… *20 n = 1 h = 1 while n <= 20:
h *= n
n += 1 print h```

8. To calculate and to print sum 50+48+46+44+… +20

```to ###Calcular and to print sum 50+48+46+44+… +20 n = 50 h = 0 while n >= 20:
h += n
n - = 2 print h```

9. Program that prints the uneven nuumeros from the 100 to the unit and calculates its extreme one n = 100

```###Programa that prints the uneven nuumeros from the 100 to the unit and calculates its extreme one n = 100 h = 0 while n >= 1:
if n%2! = 0:
print n,
h += n
n - = 1 print ‘and its sum is: %i’ % h```

10. To introduce a nuumero by keyboard and to say if he is even or uneven

```### Introducir a nuumero by keyboard and to say if he is even or uneven h = input (‘Introduces I number: ’) if h%2 == 0:
print ‘This I number is even’ else:
print ‘This I number is uneven’```

11. To print the numbers from the 1 to the 100 and to calculate the sum of all the even nuumeros on the one hand, and on the other, the one of the odd numbers.

```to ##Imprimir the numbers from the 1 to the 100 and to calculate the sum of all the nuumeros ###pares on the one hand, and on the other, the one of the odd numbers.
n = 1 p = 0 i = 0 while n <= 100:
print n,
if n%2 == 0:
p += n
else:
i += n
n += 1 print ‘\ nLa sum of the pairs is equal to %i’ % p print ‘the sum of the odd numbers is equal to %i’ % i```

12. To introduce two numbers by keyboard. To print the numbers that there are among them beginning by but the small one. To count whatever has and whatever of them are even. To calculate the sum of the pairs

```to ##Introducir two numbers by keyboard. To print the numbers that there are among them ###comenzando by but the small one. To count whatever has and whatever of them are ###pares. To calculate the sum of the even ones p = 0 cp = 0 c = 0 n = 0 h = 0 h1 = input (‘First I number: ’) h2 = input (‘Secondly I number: ’) if h1 > h2:
n = h2
h = h1 else:
n = h1
h = h2 while n < h:
n += 1
if n == h:
break
c += 1
print n,
if n%2 == 0:
cp += 1
p += n print ‘\ nEntre % i and %i is %i even numbers being %i’ % (h1, h2, c, cp) print ‘the sum of the pairs is %i’ % p```

13. To print and to count the multiple numbers of 3 that there are between 1 and 100.

```### Imprimir and to count the multiple numbers of 3 that there are between 1 and 100. n = 1 h = 0 while n < 100:
if n%3 == 0:
print n,
h += 1
n += 1 print ‘\ nEntre 1 and 100 is %i multiple numbers of 3’ % h```

14. To print, to add and to count the numbers that are simultaneously multiple of 2 and of 3, which there is between the unit and determining I number introduced by the keyboard.

```### Imprimir, to add and to count the numbers that are simultaneously multiple of 2 and ###de 3, that there is between the unit and determining I number introduced by the ###teclado one.
n = 1 num = input (‘To even arrive: ’) h = 0 sum = 0 while True:
if 3*n > num:
break
if (3*n) %2 == 0:
print 3*n,
h += 1
sum += 3*n
n += 1 print ‘\ nEntre 1 and %i is %i multiple numbers of 3 and of 2’ % (num, h) print ‘These numbers add %i’ % sum```

15. To introduce two values To and B: S.A. =B,

to calculate and to print sum 10+14+18+… +50 If A/B<=30,

to calculate and to print the value of (A^2+B^2)

```to ##Introducir two values To and B:
# # # S.A. =B, to calculate and to print +50 sum 10+14+18+… ###Si A/B<=30, to calculate and to print the value of (A^2+B^2) a = input (‘First value: ’) b = input (‘Secondly value: ’) n = 10 sum = 0 sum = 0 if to >= b:
while n <= 50:
sum += n
n += 4
print sum if a/b <= 30:
sum = (to ** 2+b ** 2)
print extreme```

16. To introduce the values To, B and C:  If A/B>30,

to calculate and to print A/C * B^3      If A/B<=30,

to calculate and to print 2^2+4^2+6^2+… +30^2

```to ##Introducir the values To, B and C:
###Si A/B>30, to calculate and to print A/C * B^3 ###Si A/B<=30, to calculate and to print 2^2+4^2+6^2+… +30^2 a = input (‘First value: ’) b = input (‘Secondly value: ’) c = input (‘Third value: ’) n = 2 sum = 0 sum = 0 if a/b > 30:
sum = a/c*b ** 3
print sum if a/b <= 30:
while n <= 30:
sum += n ** 2
n += 2
print extreme```