Author Archives: tywins

Validated user input in Python

When it comes to validated user input remember it depends on what you’re gonna use the input for, for example if you want to ask the user for a number to do a sum, you need to add int (for integer) or float (for floating point numbers).

Validated user input in Python

Another example would be in a loop, where the use of characters is often used. The point is to always specify what sort of variable the program is expecting to receive.

Validated user input in Python

User input (text based) in Python (basic)

To create a user input, you need to type input then inside parenthesis and quoting marks you ask for the user whatever piece of info you need to get your program running.

User input (text based) in Python (basic)

User input (text based) in Python (basic)

Creation and use of strings in Python

To create a string you simply need to put the text inside quoting marks (“”), because that’s just how Python works. Also you can put inside variables so it’s easier to print them whenever you want to instead of writing all the text all over again I think. Yeah, anyway here’s an example:

Creation and use of strings in Python

Because Machamp has 4 arms.

Creation and use of strings in Python

Creation and use of tuples in Python

Creation and use of tuples in Python

Now here’s an example of what a tuple looks like, it’s very similar to a list, but the difference is that you cannot edit tuples, that’s why it’s telling you that there is an error at the end, because in line 6 it’s telling the program to delete the first element of the list but since it’s a tuple you cannot edit it to not show you that element, you could of course create another tuple with only the desired elements since you cannot edit it. Also the tuple uses parenthesis instead of brackets like in a list.

Creation and use of tuples in Python

Creation and use of lists in Python

Here’s a very basic example of a list and a bit of what you can do with it.

Creation and use of lists in Python

You can see the entire list when you print it, but you can also choose which of the elements inside the list you want to show by setting a range (exclusive). Always remember the lists starts off by 0, so if you wanted to only show the last three colors you’d need to set the range from 2 to 5.

Creation and use of lists in Python

Also, you can delete certain elements from the list, so no matter what range or whether or not you want to print the entire list, the element you’re deleting will not be shown.

Creation and use of lists in Python

When to use what type of repetition in a program

When you need the loop to go on and on until the conditional is false, it’s convenient to use a while loop. When you have a predefined range that works as the conditional in a program you need to use for loops, finally when you need to call a function inside the same function it’s called a recursion, it makes it simpler but not always works as efficiently, each of these have different purposes, it’s a matter of knowing which suits the logic of the program best.

Here’s an example of each:

When to use what type of repetition in a program

When to use what type of repetition in a program

When to use what type of repetition in a program

When to use what type of repetition in a program

Use of recursion for repetitive algorithms

Recursive functions are very useful when it comes to repetitive algorithms, it makes it not only easier to understand but cleaner and more practical. It’s basically conditional statements inside a function. A recursive function has to terminate to be used in said program.

For example, in a fibonacci program, we use recursion so it evaluates the number, if it’s equal to either 0 or 1, it will return the same number, else it returns the answer:

Use of recursion for repetitive algorithms

Use of recursion for repetitive algorithms

Nesting of conditional statements

Nesting of conditional statements

Ok the first thing I did here was setting a variable to store the input given by the user, depending on the user’s answer is the course of action of the program, basically if the user says they do want pizza they get sent to the first if statement, which then asks them which flavour of pizza they prefer, after that the program responds as I’d probably react, because pepperoni pizza is way too overrated in my opinion, else hawaiian pizza is for cool risk takers like me to be honest, and I need more people who can handle pineapple on a pizza in my life. Finally, if the user says they don’t want any pizza in the first place they get sent to the else statement that asks them if they’re okay.

Pretty simple way of understanding the logic of nesting conditionals, so you’re welcome I guess.

Nesting of conditional statements

Importing and using Python modules

Modules are like preset functions within Python. For example if I want to get the square root of a number but I don’t want to do it in the babylonian way or whatever, I can just import a math function that does all the nasty work for me. In this case I’d need to type “import math” at the very start of my code. Then use the square root module (math.sqrt), here’s an example:

Importing and using Python modules

Importing and using Python modules

Calling Python functions

The only thing you need to do to call a function is store it into another variable, so you can then print it with the main variable in which the function is being stored in.

Calling Python functions

Calling Python functions