WSQ04 – Sum of Numbers

--Originally published at Hello World

Here are the instructions:

Write a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10.

Notice our sum starts with zero (why?) and then we add each number in the range provided by the user. Just for fun, what is the mathematical formula to do this calculation?

 


Mastery Topics Needed To Be Covered

--Originally published at Hello World

Transversal Topics

  • Ability to create Python/C++ file and run from command line
  • Create accounts: Blog, Twitter, GitHub
  • Submit work via Blog RSS and GitHub
  • Demonstrate use of Linux sufficient for quizzes/exams
  • Install Linux on their own computer
  • Ability to create Python/C++ project in IDE and run inside the IDE (advanced topic, not needed early)

Common #TC101 Topics

  1. Use of comments
  2. Python conventions (Zen of Python but others for other languages)
  3. Basic types and their use
  4. Basic output (print)
  5. Basic user input (text based)
  6. Calling functions
  7. Creating functions
  8. Importing and using modules/libraries
  9. Creating and using your own modules/libraries
  10. Use of the conditional “if”
  11. Use of “else” with a conditional (and elif for Python)
  12. Nesting of conditional statements
  13. Use of loops with “while”
  14. Use of loops with “for”
  15. Use of recursion for repetitive algorithms
  16. When to use what type of repetition in a program
  17. Creation and use of Lists/Tuples in Python
  18. Creation and use of strings
  19. Validated user input (ensure correct/expected data entry)
  20. Reading and writing of text files
  21. Creation and use of ranges in Python
  22. Creation and use of dictionaries in Python

#Quiz04

--Originally published at Hello World

Here are the instructions:

  • def minimum_three(x, y, z):  # returns the value that is smallest of x, y and z
  • def sum_squares(x, y, z): # returns the value of the sum of squares of x, y, z

 

Writing your code in normal fashion is important, thus start defining your functions at the top, then you can ask the user for inputs.

  1. For the smallest value of the three numbers, we just have to use the function min() defining the smallest value of the numbers asked to the user.
  2. As I have mentioned in a previous blog post, the function pow(x, 2) squares your integer. Therefore, we just have to define the function sum_square, calling pow for each integer, then adding up together. Finally you just need to return the value.

#WSQ 02 – Temperature

--Originally published at Hello World

Here are the instructions:

Write a program that will prompt the user for a temperature in Fahrenheit and then convert it to Celsius. You may recall that the formula is C = 5 ∗ (F − 32)/9. Modify the program to state whether or not water would boil at the temperature given.

  1. We know that the water boils at 100°C.
  2. We write the input function asking for the temperature Fahrenheit without forgetting to convert the string into an integer in order to do mathematical operations with it!
  3. Then easy, just need to convert Fahrenheit into Celsius using the formula given C = 5 ∗ (F − 32)/9. Once again, we need to convert the string of text Celsius into an integer.
  4. Here is the novelty: the conditional execution. I found the table summarising the comparison operations on https://docs.python.org/2/library/stdtypes.html
  5. We know that water boils only if the temperature of the water is greater or equal than 100; otherwise the water does not boil
  6. I used this tutorial to see how to write the if/else statement properly https://www.tutorialspoint.com/python/python_if_else.htm

 


#Quiz03 Square Root, Cube Root

--Originally published at Hello World

Functions

For this quiz I want you to (in class) create a program with two functions:

  • def square_root(x):  // returns the square root of x (float)
  • def cube_root(x): // returns the cube root of x (float)

 

What to Do

You implement this function in your own program in a file quiz3.py.

You should make a main routine that asks the user for a number and then calls your functions to calculate the square and cube roots of that number and prints them out.

What should you do if the user enters a negative number?

 

 

 

First, you need to write “from math import sqrt” : it means you can import functions from the math module.

A function is a named sequence of statements that performs a computation.

When you define a function, you specify the name and the sequence of statements. By writing “def square_root (x)”. Then your function will return a result: it is called the return value.

def : indicates that it is a function definition

square_root : is the name of the function

(x): x is the argument of the function

 

Return is what comes out of my function.

 

 

 


#WSQ01 = Watch, Summarise, Questions

--Originally published at Hello World

Fun With Numbers

Here I will try to explain how I managed to finish the first assignment.

Here are the instructions:

Ask the user for two integer values, then use those two values to calculate and show the following:

  • The sum of the two numbers.
  • The difference of the two numbers.
  • The product of the two numbers.
  • The integer based division of the two numbers (so no decimal point). First divided by second.
  • The remainder of integer division of the two numbers.

As I am a “noob”, first I had to look for the definition these terms:

The user: A system user is a person who interacts with a system, typically through an interface, to extract some functional benefit.

An integer: A type that represents whole numbers.

As said in class, this assignment covers the basic user input (text based) and basic output (print).

First, I choose the two integer 5 and 4, which gives

x = 5

y =4

Then, we want to do arithmetic, thus we need to convert the strings of numbers to integer.

Here is the tutorials I used:

https://www.tutorialspoint.com/python/arithmetic_operators_example.htm

http://stackoverflow.com/questions/5584586/find-the-division-remainder-of-a-number

 

 


The First Line of Code

--Originally published at Hello World

There we go, second class the teacher helped us to set up our programming environment. It is actually pretty easy, just needed to set up Python 3. First, we had to install HomeBrew and then Atom in order to be able to run the code. There we are.

Then it is pretty easy. Open Atom and go under ” Untitled “. You can start writing on the first line. The function “print () ” will display your text. The text you want to be displayed need to be inside two quotation marks ( either “…” or ‘…’ ).

We want the classic programming sentence ” Hello, World ” to appear. Just need to write this exactly

print (“Hello, World”)

Do not forget to save it with the extension “HelloWorld.py” because it tells the computer and Atom that this is a python file.

Then from the Terminal you just need to write

cd Desktop/

python3 HelloWorld.py

And voilà!