Author Archives: tywins

Creating Python functions

First you define the name of the function and add in parenthesis the variable you’re gonna use, then you must equal different variables to the ones you’re gonna use for your program, after that you can create the logic of the function, or what it’s supposed to do with said variables, finally you can return the result.

Creating Python functions

Creating Python functions

Basic output (print) in Python

Basic output (print) in Python

Basic output (print) in Python

Basic types and their use in Python

Variables in Python are there to store values, there are different types of variables, but the basic ones are integers, floating point, strings (text). The program has five standard data types: numbers, string, list, tuple (the main differences between lists and tuples is that tuples cannot be updated, unlike lists, also you use brackets with lists and parenthesis with tuples) and dictionary (stores data that can be shown pressing the key/phrase that has been assigned to it).

Here’s an example of the use of variables in Python:

Basic types and their use in Python

Basic types and their use in Python

Python conventions (Zen of Python)

Zen of Python is a mini style guide for Python code, kind of a humorous way to see it?, you can either google it or open your editor and type “import this”, once you’ve saved it, go to the terminal and open the python program, it should look a bit like this:

Python conventions (Zen of Python)

Python conventions (Zen of Python)

Demonstrate use of Linux sufficient for quizzes/exams

We’ve made several quizzes in class that require the use of Linux. The professor hands out assigned USBs which I suppose contain the program, we turn off the computers, plug in the USB and simultaneously turn the computers on again, pressing F12 continuously, after that we select the first option, finally we register ourselves and get ready to start either a quiz or practice.

Demonstrate use of Linux sufficient for quizzes/exams

Submit work via Blog RSS and GitHub

As showed throughout this course, every single assignment must be delivered via RSS, using the blog and github. So the first thing we were instructed to do was to create a blog in any social network such as Tumblr, WordPress, Blogger, or any blogging website that works similarly.

Once we had created and verified it, we made sure we were registered in Ken’s blog for the course, that way we could link our blogs to his. The next step was creating posts and publishing content which would later appear on Ken’s blog through the RSS and tagging system.

This way there is always control on which assignments were delivered, when, and in which group. Also to make it more organized we created a repository in Github, this way every piece of code we get done is inside a folder that syncs with the website, then you can link your codes in your posts.

Here‘s the video Ken did to explain better.

Submit work via Blog RSS and GitHub

Lists – WSQ10

Lists – WSQ10

The program must ask the user for ten numbers, tell the user they cannot enter any more values, then print the total sum, average and standard deviation of these. So, first, to make sure the user is only able to enter 10 values, you have to set up a loop, in this case I used the while loop, in which, as long as the counter x is lower or equal to 10 (except it respected the 10 values permitted only with < rather than <= or changing it to 11, so 10 it is), it will continue to receive inputs.

Onto the list, I opted to define the list outside the loop as an empty list, that way the variable that contains the numbers given by the user will be appended to l (the list), it allows me to save myself from creating a long chunk of code with lists that would contain each input, also it’s cleaner and more organized this way for me.

After that, since we want to do this using functions because functions are fun, we’re defining the first one (this was explained in the previous entry pal), I’m using another counter here for the total sum, so I can add to it the value of m (which is the equivalent for l, below) for as long as the loop goes on, to make sure the function is gonna do its job no matter how many numbers the user is permitted to enter, we indicate a range, which will depend on the length of m. When you’re thorough with that, make sure to add a variable that contains that function, below the loop. All that business was only for the total sum of the numbers.

For the average of these, we create another function, since we did most of the work in the first function, all we need to do now is use the variable that contains the total sum function and divide it by 10.0 (since it’s a float value), to obtain its average. We do the same with this function and save it in another variable below the total sum.

Finally, there is actually a specific preset function for standard deviation, but before you use it you must import it at the very beginning of your code, this way you can now use it in the list, I’m not entirely sure why I couldn’t use the variable for the average function that I saved below in this one though, it only worked for me directly using l (for list), also when it comes to indexation, I had to take this one out of the loop otherwise it wouldn’t work.

And that’s basically it, the last part is just printing the results on screen, always remembering to use the names of the variables that contain their respective functions rather than said functions or variables inside these.

Github code link.

Lists – WSQ10

Factorial Calculator – WSQ09

Factorial Calculator – WSQ09

This program asks the user for a number and prints the factorial result. It uses a function in which, as long as the variable a is a positive number, then it does the product of b (which has the value of 1) and a (given by the user), using a loop to stop the program when a is equal to itself.

The purpose of the if – else conditional is to make sure, once again, the user is giving the program a positive integer, if the number inserted is lower than 0, the program will ask the user to try again with a positive integer.

To asure this program keeps running while the user wants it to, we use another loop, defining the value for as the character (‘y’), so after it prints the answer it also asks the user if they’d like to try another number, the program will only do the loop if the user answers with y.

Github code link.

Factorial Calculator – WSQ09

Fun with Numbers (Function) – WSQ08

Fun with Numbers (Function) – WSQ08

First, you need to define the function, and state the variables you’re gonna use in parenthesis, they have to be different from the ones you’re gonna use to ask the user the value of these, then you do the operation under another variable, when you are done with this, type return and the name of the variable that contains said operation. You must do this for every function you create.

After that, ask for the original variables, the ones that are supposed to be different from the ones you initially did the function(s) with, create one last variable that will be equal to the name of the function you want, finally print the result using the variable you just created, which contains/is equal to the function.

Github code link.

Fun with Numbers (Function) – WSQ08

Creation and use of ranges in Python

Creation and use of ranges in Python

This program lists a range of numbers given by the user, then allows you to see the series of said range that goes up by an integer also introduced by the user (again, not inclusive).

So, you can tell the program that you want to see the range from 2 to 10. And you can also specify that you want to see the list of numbers but instead of going up one by one, you could command it to show you the range but every 2 numbers, for example, instead of printing “2, 3, 4, 5, …”, it will print “2, 4, 6, 8, …”.

Creation and use of ranges in Python