Hey there! On this blogpost I’ll ulpoad the guide I did to study for my partial exam.
I read “Beginning Python”by Peter C. Norton, Salez Samuel, Dave Aitel, Eric Foster-Johnson, Leonard Richardson, Jason Diamond, Althea Parker and Michael Roberts.
I’ve been using this book during all the partial and it’s been really useful.
Well, here are my answers:
- How do we create comments and why?
With the # symbol. We write them through the program to indicate it’s development or just to write notes. Sometimes they’re used to skip parts of the code that are no longer needed.
- Explain the basic types and why there are different ones.
Data Types are categories for things within a program with which the program will work. There are different ones because you won’t use the same kind of objects every time you make a code. For example, if you need a number with a decimal part you’ll use a float, if you need an integer number, you’ll use the “int” type. After a thing has a type, the program and the programmer knows what to do with it.
- How do we do basic input and output (text)?
Most of the times is necessary to assign a variable to each input so we can manipulate the information in the code. The “input” syntax looks like this:
X= input (“text”)
I always output information with the “print” command. The syntax is this:
print (“text”). If you’re adding variables to your output sentence, the syntax will be (“text” , variablenumber, “text”)
- How do you call functions?
When we have a function already defined, we assign a variable to it, and to call it, we just write: print (variable assigned) and our function will be executed.
- How do you define/create functions?
With the keyword “def” followed by the name that you want to give to your function. Be careful that you don’t give it the same name than another keyword or type in python, or your program will have some problems.
And then with the keyword return, where we write what that function will do
- How do you import and use libraries/modules?
Modules and libraries are a bunch of functions defined by the program or yourself, it depends on what you need to do in the current program you’re making. To call one of them, you need the keyword form (module) import (name of the function). Some of them just need the import keyword, and when you need the function, a dot and the specific action form that library. For example mat.sqrt. This will give you the square root of a number. Or the “from” random import randint will give you a random number in the range that you indicated. It depends on the module/library if you need the “form” keyword.
- Explain conditionals and usage of if, else (and elif for Python)
They’re needed to make decisions. They can be understand easier if we do an example with a flowchart.
“if” else and elif will allow the program to decide if a condition is true or not. If: evaluates the expression for the first time, if it’s not true, the program will go to run the Elif clause, and if both of them are false, the program will run the else clause.
The number of keywords that you’ll use in your program is equal to the number of conditions that you have in your code.
- Explain the workings and use of while loops
While loops are really useful when you’re programming a code that needs a part to run several times. The “while” will do what you indicate the program to do WHILE the condition is true. I’ll give an easy example. We’ll print the range number form 1 to 6.
Print: (“We’re done”)
So what the while the condition is true, the loop will run and the cont variable will increment it’s value each time the loop gets executed.