Author Archives: Gilberto Rogel García

#mastery13 Importing and using Python modules

An example of this can be found on my WSQ06, here’s the part of the code where i imported a module:

import random imported the module random that in this case it produces a random intenger from a certain parameter 

value= random.randint(1,100) this line does is that the variable value will become a random integer (random.randint) from 1 to 100 (1,100)

print(value)

This program will show a random value from 1 to 100.

Another example is:

import math import the module math which has lots of functions that can be found here https://docs.python.org/2/library/math.html

pi=math.pi will take math.pi’s value which  is equivalent to 3.1416…

print (pi)

This program will show:

3.141592653589793

#mastery12 Creating python functions

I used this link to help me do this mastery: https://docs.python.org/2/tutorial/controlflow.html#defining-functions

It is also useful to read chapter 3 of the book “Think Python, How to Think Like a Computer Scientist”, 

You can see an example of this in my . Here’s the code showing the function that i created

def osuma (num1,num2): you have to define your function, in this case it’s osuma with num1 and num 2 as parameters

suma=num1+num2 is what the function does… this line must be idented so the function can work properly

return suma  #the return statement returns with a value from a function… this line must be idented so the function can work properly

 

num1 = int(input(“Give me a number: “)) 

num2 = int(input (“Give me another number: “))

su=osuma(num1,num2) i called osuma function i defined in order for the variable to take the function’s value

 

print (“The sum of your numbers is”, su) 

 

#mastery11 Calling python functions

I used this link to help me do this mastery: https://docs.python.org/2/tutorial/controlflow.html#defining-functions

It is also useful to read chapter 3 of the book “Think Python, How to Think Like a Computer Scientist”, 

You can see an example of this in my . Here’s the code showing where i called the function that i defined:

def osuma (num1,num2): osuma

suma=num1+num2

return suma 

 

def oresta (num1,num2): oresta

resta=num1-num2

return resta

 

def omulti (num1,num2): omulti

multi=num1*num2

return multi

 

def odiv (num1,num2): odiv

div=num1/num2

return div

 

def orem(num1,num2): orem

rem= num1%num2

return rem

 

 

num1 = int(input(“Give me a number: “)) 

num2 = int(input (“Give me another number: “))

i called every function i defined in order for the variable to take the functions value

su=osuma(num1,num2) i called osuma function i defined in order for the variable to take the function’s value

res=oresta(num1,num2) i called oresta function i defined in order for the variable to take the function’s value

mult=omulti(num1,num2) i called omulti function i defined in order for the variable to take the function’s value

di=odiv(num1,num2) i called odiv function i defined in order for the variable to take the function’s value

re=orem(num1,num2) i called orem function i defined in order for the variable to take the function’s value

 

print (“The sum of your numbers is”, su) 

print (“The difference of your numbers is”, res) 

print (“The product of your numbers is”, mult) 

print (“The division of your numbers is”, int(di)) 

print(“The remainder of the division of your numbers is”, int(re)) 

 

#mastery10 Basic output (print) in Python

To show a message on python you have to use the print function which is probably the easiest, here’s an example:

print (“Hello World”) have to put “Hello World” between quotation marks because it is a string 

This will show the user:

Hello World

You can also print variables with this function, for example:

num= 1

print (num) quotation marks because num is a variable that contains a number

This will show the user:

1

BUT if you put num= helloworld this will show an error that says that helloworld is not defined because the program will take helloworld as another variable. If you want to add a string to a variable it must be between quotation marks like this:

num= “helloworld”

print (num)

This will show the user:

helloworld

🙂

 

#mastery05

We already practiced in class how to progam in python using Linux with Ken’s USB keys so we can get used to the exam environment of this course.

 

#mastery05

We already practiced in class how to progam in python using Linux with Ken’s USB keys so we can get used to the exam environment of this course.

 

#TC1014 #WSQ08 On to functions

For this WSQ i had to read chapter 3 of the book ”  “Think Python, How to Think Like a Computer Scientist” which explained how to make functions.

Here’s my code:

ROGEL GARCÍA A01630171

def osuma (num1,num2):

suma=num1+num2 line must be idented in order for the function to work properly.

return suma  #this line must be idented in order for the function to work properly.

 

def oresta (num1,num2):

resta=num1-num2 #this line must be idented in order for the function to work properly.

return resta  #this line must be idented in order for the function to work properly.

 

def omulti (num1,num2):

multi=num1*num2  #this line must be idented in order for the function to work properly.

return multi  #this line must be idented in order for the function to work properly.

 

def odiv (num1,num2):

div=num1/num2

return div

 

def orem(num1,num2):

rem= num1%num2  #this line must be idented in order for the function to work properly.

return rem  #this line must be idented in order for the function to work properly.

 

 

num1 = int(input(“Give me a number: “)) 

num2 = int(input (“Give me another number: “))

 

su=osuma(num1,num2)

res=oresta(num1,num2)

mult=omulti(num1,num2)

di=odiv(num1,num2)

re=orem(num1,num2)

 

print (“The sum of your numbers is”, su) 

print (“The difference of your numbers is”, res) 

print (“The product of your numbers is”, mult) 

print (“The division of your numbers is”, int(di)) 

print(“The remainder of the division of your numbers is”, int(re)) 

#TC1014 #WSQ08 On to functions

For this WSQ i had to read chapter 3 of the book ”  “Think Python, How to Think Like a Computer Scientist” which explained how to make functions.

Here’s my code:

ROGEL GARCÍA A01630171

def osuma (num1,num2):

suma=num1+num2 line must be idented in order for the function to work properly.

return suma  #this line must be idented in order for the function to work properly.

 

def oresta (num1,num2):

resta=num1-num2 #this line must be idented in order for the function to work properly.

return resta  #this line must be idented in order for the function to work properly.

 

def omulti (num1,num2):

multi=num1*num2  #this line must be idented in order for the function to work properly.

return multi  #this line must be idented in order for the function to work properly.

 

def odiv (num1,num2):

div=num1/num2

return div

 

def orem(num1,num2):

rem= num1%num2  #this line must be idented in order for the function to work properly.

return rem  #this line must be idented in order for the function to work properly.

 

 

num1 = int(input(“Give me a number: “)) 

num2 = int(input (“Give me another number: “))

 

su=osuma(num1,num2)

res=oresta(num1,num2)

mult=omulti(num1,num2)

di=odiv(num1,num2)

re=orem(num1,num2)

 

print (“The sum of your numbers is”, su) 

print (“The difference of your numbers is”, res) 

print (“The product of your numbers is”, mult) 

print (“The division of your numbers is”, int(di)) 

print(“The remainder of the division of your numbers is”, int(re)) 

#TC1014 #WSQ00 Sign page one

Succesfuly signed page one. 

You can check my comment here: http://ken.baueralonso.com/courses/course-home-page-for-tc1014/tc1014-page-one/#comment-3

 

#TC1014 #WSQ01 Setup python

Installed python 3 for my programming class 😉 

I downloaded it from this link: https://www.python.org/downloads/