Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views4 pages

Python Examples

This document presents 11 programming exercises in Python that cover topics such as: input and output of data, conditionals, loops, functions, strings, lists, and arrays. The goal is for the student to become familiar with the Pycharm development environment and learn basic concepts of Python syntax.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Python Examples

This document presents 11 programming exercises in Python that cover topics such as: input and output of data, conditionals, loops, functions, strings, lists, and arrays. The goal is for the student to become familiar with the Pycharm development environment and learn basic concepts of Python syntax.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LABORATORY #1A

SWITCHING

OBJECTIVE

Familiarization with the Pycharm programming environment and first steps with the syntax of
programming in Python.

ACTIVITY

Set up a project in Pycharm, named PracticaLab1, execute each of the following


codes and document the lines of each routine (the comment in Python is defined using #).

input('Write a phrase: ')


while chain != '':
cambios =0
for i in range(1, len(string)):
if string[i] == ' ' and string[i-1] != ' ':
changes = changes + 1
if string[-1] == ' ':
changes = changes - 1
words = changes +1
print 'Words:', words
Enter a phrase:

2) ______________________________________________________________________

#!/usr/bin/env python

from math import pi


Give me the radius:
area = pi * radius ** 2
print 'The area of a circle with radius %f is %f' % (radius, area)
print 'The area of a circle with a radius of %6.3f is %6.3f' % (radius, area)

3)_______________________________________________________________________

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from math import sqrt, asin, pi
def area_triangle(a, b, c):
s = (a + b + c) / 2.0
returnsqrt(s * (s - a) * (s - b) * (s - c))
defangulo_alfa(a, b, c):
s = triangle_area(a, b, c)
return 180 / pi * asin(2.0 * s / (b * c))
defmenu():
opcion =0
while option != 1 and option != 2:
1) Calculate the area of the triangle
2) Calculate the angle opposite the first side
option = int(input('Choose option: '))
return option
side1 = float(input('Give me side a: '))
side b:
float(input('Give me side c: '))
s = menu()
ifs == 1:
result = triangle_area(side1, side2, side3)
else:
alpha_angle(side1, side2, side3)
You chose the option, s
print 'The result is:', result

4)___________________________________________________________________________

#!/usr/bin/env python

carga50 =1000
carga20 =1000
carga10 =1000
def withdraw_money(amount):
globalcarga50, carga20, carga10
if amount <= 50 * load50 + 20 * load20 + 10 * load10:
de50 = quantity / 50
amount = amount % 50
ifde50 >= carga50:
amount = amount + (from50 - load50) * 50
charge50
amount / 20
amount = amount % 20
ifde20 >= carga20:
amount = amount + (from20 - load20) * 20
load20
de10 = quantity / 10
amount = amount %10
ifde10 >= carga10:
amount = amount + (from10 - load10) * 10
load10

if quantity == 0:

load50 = load50 - from50


load20 = load20 - from20
load10 = load10 - from10
return[de50, de20, de10]
else:
return[0,0,0]
else:
return[0,0,0]
Main program
while 50*carga50 + 20*carga20 + 10*carga10 > 0:
request = int(input('Amount you want to withdraw: '))
withdraw_50
if[de50, de20, de10] != [0,0,0]:
ifde50 >0:
Print '50 euro notes:', de50
ifde20 >0:
print '20 euro notes:', de20
ifde10 >0:
print '10 euro bills:', de10
Thank you for using the ATM.
print
else:
We regret that we cannot fulfill your request.
print
ATM out of money. Please notify maintenance.

5) ______________________________________________________________________________

#!/usr/bin/env python

n = int(input('Give me a number: '))


m = int(input('Give me another number: '))
ifn * m == 100:
print 'The product %d * %d is equal to 100' % (n, m)
ifn * m != 100:
The product %d * %d is different from 100

6)________________________________________________________________________________

i = 0
while i < 7:
print
i += 1
Done

for i in range(1,6):
printi

#!/usr/bin/env python

bits = input('Give me a binary number: ')


valor =0
forbitinbits
ifbit == '1':
value = 2 * value + 1
else:
value = 2 * value
print 'Its decimal value is', value

9)_____________________________________________________________________________

for i in range(0, 2):


for in range(0, 3):
print, j

10)_____________________________________________________________________________

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def null_matrix(rows, columns):
M = []
for in range(rows):
M.append( [0] * columns )
returnM
deflee_matrix(rows, columns):
M = null_matrix(rows, columns)
foriinrange(rows):
for j in range(columns):
M[i][j] = float(input('Enter the component (%d,%d): ' % (i, j)))
returnM
this is a comment
defdimension(M):
return[len(M),len(M[0])]
defsuma(A, B):
if dimension(A) != dimension(B):
returnNone
else:
[m, n] = dimension(A)
C = null_matrix(m, n)
foriinrange(m):
for in range(n):
C[i][j] = A[i][j] + B[i][j]
returnC
a = input('Enter number of rows')
b = input('Enter number of columns')
C = null_matrix(a,b)
D = null_matrix(a,b)
C = read_matrix(a, b)
D = read_matrix(a, b)
E = sum(C,D)
printC
printD
printE

11)__________________________________________________________________________

defmin(a, b):
if a < b:
return
else:
returnb
defmax(a, b):
ifa > b:
return
else:
returnb
if __name__ == '__main__':
The maximum of 3 and 10 is 10
The maximum of 3 and -10 is 3
print 'The minimum of 3 and 10 is', min(3,10)
print 'The minimum of 3 and -10 is', min(3,-10)

You might also like