## 16 Examples of Python + Codes

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

Â