Asking questions when learning something new is one of the most efficient and important ways to get started, as most people aren’t going to know exactly how everything works. While learning something new, many people will ask questions like “How do x, y, and z work?” Just by asking the question, they are starting the learning process. Taking the time to learn Python code isn’t something that will come quickly with just reading an article off the internet. While you’ll get a base understanding of how things work, finding places that answer those little but helpful questions in Python make the process easier.
Over the years, more people have started to find interest in learning code, and with all those new beginners came more questions. Some of the frequently asked questions that are most important but little questions when learning Python are what exactly is Python? How do you convert a string into a number? What are negative indexes, what are functions, etc.? Knowing where to look when you have questions can be one of the most important things to know when learning Python or any coding language.
What is and why Python?
In most public school systems, one of the many requirements to graduate high school is taking two years of a new language, such as Spanish or French. The main difference with learning Python is that it’s a coding language, where you tell a computer to do something, rather than a language, where you say to a person something. Despite their differences, similar rules can still apply. There are certain built-in functions that you can code, which mean and do different things, just like translating words from one language to another.
What are Functions?
The word function will come up multiple times when reading or watching most things about Python. The word Function in Python can define various actions, but when focusing on the definition of functions, they’re just a statement that performs a specific task. Someone new to Python may not know the exact meaning of a function, but simple things such as print() and sqrt() are functions. There tend to be two different kinds of functions, the standard library function and a user-defined function. With the standard library, it’s just the types of functions that are built into Python itself. A user-defined function is a tool in Python to create your functions.
Creating a function can be an essential part of coding when you want to reuse the same piece of code multiple times, which can keep the programmer from having to retype or copy and paste the same code. When a programmer wants to create their function, they’ll use the keyword “def” followed by a space and what they want to name their function. In the following line, you’ll define what the function does. Parameters can be helpful when needing to add arguments, but parameters are optional. When using arguments, if there are two previously defined arguments in the parameters, when calling the function later, it is necessary to input two values so the code can run properly. After defining the argument, naming it, and adding the required parameters, you’ll want to end the function with a colon (:) to show the end of the function header.
The photo below shows an example of how to define a function properly:
Looking at the photo, you’ll see a body, and within it is a return statement. Using a return statement is optional, but if wanting to run the code multiple times, it’ll replace the original value of b with the new value of b. In the code above, it isn’t necessary to run it multiple times.
Converting String to an Integer
Strings and Integers are used when using the input() function and are something a new programmer should get comfortable using. Using inputs in Python, they’ll automatically start with being a string, so it won’t be able to compute a mathematical problem. So, look at the code below, assuming that the input is going to be 10:
x = input()
print(x + 5)
When first starting Python, your first response is thinking it’ll print 15. Except with how Python automatically reads inputs as strings, when running the code, it’ll present you with a ‘Type Error’ saying how there is an unsupported operand with ‘+’. While for new programmers, it may seem like a more significant problem, the solution is as simple as typing in an extra five characters. Look at the code below, assuming that the input is going to remain as 10:
x = int(input())
print(x + 5)
The only thing that changed between the two was the added int () function; as seen this time when running the code, it’ll print out the desired output of 15. This built-in function is short for integers, which makes the Python code able to compute mathematical problems. The function will take whatever is inside the two parentheses (generally referred to as parameters) and convert it into an integer. A common mistake when coding with int() is not to put the extra parenthesis at the end. While a little mistake, when it’s in an extensive program, it can become challenging to find when an error occurs. A good practice is to run a code every few lines to avoid this mistake and other mistakes.
List vs. Tuple
Looking at a list and a tuple, they seem almost identical to how both are used for storing objects in Python. When looking at how both work, there’s only one thing that separates one from another: Lists are mutable, and Tuples are immutable. This means that when you have a list, you can change what’s in it, but with Tuples, you cannot edit what’s in the list after it’s made. For new programmers being able to change what’s in a list may seem useless; why would you not just rewrite what’s in the code if you need to change it? The simple answer is when you’re using lists within if-else statements.
The video below shows how lists fit in with if-else statements.
list_ex = [11,56,23,76]
tuple_ex = (12,57,24,77)
Comparing the two examples, the only noticeable difference is how lists use [ ] to define what’s in them, while a tuple uses ( ). Looking further than how lists and tuples look is helpful when deciding which one to use when running a code. Lists can consume more memory and run slower than a tuple, which, if a programmer needs to run multiple lists, can fill up their memory quickly, and the code won’t run as necessary. A programmer could use a tuple to have their code run quickly and when it’s unnecessary to make any changes to the list.
When deciding whether to use a tuple or list, it seems easier to pick a List in case of any changes that need to be made. Although, a program isn’t always going to require changes made. Some positives to tuples are their usefulness when a programmer’s code is only meant to be read. Pythons code is made to be more readable, so it isn’t uncommon to use Tuples when showing other people code. Also, they’re beneficial when storing information that can’t be changed, such as birthdays or where someone was born.
An index is a number assigned to a variable in a list, and while it may seem useless can be helpful with how you’ll need it to delete or add to a list. When calling an index, they can have either a positive or negative index, which may seem unnecessary, but long lists or lists that may not have a defined end can be helpful. Using a positive index is straight forwards; the items are assigned an index from left to right, starting at 0 and going till the end of the list, as seen below:
0 1 2 3 4
[“Number 1”, “Number 2”, “Number 3”, “Number 4”, “Number 5”]
-5 -4 -3 -2 -1
Looking at the example above, you’ll see under the numbers. There are also negative indexes. Unlike the positive ones, they’re assigned from right to left, starting at -1 and going down until the end of the list. Negative indexes are a small add-in with Python but can be helpful if needing to access the end of a list quickly. If wanting to focus more on indexes in general and a more in-depth explanation of negative indexes, watching “String Slicing Using a Negative Index” can help answer any questions a programmer may have left.
While and For Loops
Using loops in code can be beneficial when printing multiple lines or needing a specific action to repeat several times. Deciding on which loop to use can depend on what you’re trying to accomplish, and to choose correctly, you’ll need to understand how each one would work and which one would be best suited for how you’re going to want your code to run.
While Loops, the name is self-explanatory of how the loop runs and how it will run until its given condition is false. Its condition could be something along the lines of x < 3, so once x is greater than 3, such as when x = 4, the loop will stop running. When using While loops, a programmer can also use the else function. Like if-else statements in Python, if the condition in the while loop is false, it’ll move onto the code in the else statement. The photo below shows an example of how a while loop runs.
For Loops are commonly used with lists, tuples, and dictionaries with how they’ll iterate throughout the list until all the variables in the lists have been iterated over. Like While loops, you can use loops with an else statement, which once everything in the list, tuples, or dictionary has been repeated over with the loop, the else statement will run. Programmers can also use ranges for loops, which will print whatever number is defined in the range, except if the body of the loop changes what the range is defined as. For a more in-depth how For loops work, check out the video below.
Importance of Asking Questions
Knowing when and how to ask questions is one of the essential parts of learning something new. Struggling with a new subject is something that most people tend to do, and asking questions can help eliminate the part of learning that can feel lonely. Reaching out and asking questions on complex topics, not specifically Python, is a step to understanding something new fully. Something as simple as Googling the question can be a quick and easy way to get the answer. It’s good to remember that understanding new topics can’t be minimized to just reading an article or watching a YouTube video; it’s about the consistency of working on it.
Frequently Asked Questions is something new programmers should be open to reading, as it’s one of the best ways to see how other people were also starting from the same place. Looking at what Python is, Lists and Tuples, Functions, Loops, etc., show new programmers that other people didn’t start just knowing Python and how everything in it works.