Coding w/ Python — part 3
Welcome back to coding with python. In this article, we are covering operators, Loops, Functions, and arrays.
Last time we learned a little bit more about the tools that we can utilize during the coding process. They are common in a lot of coding languages and are pretty transferable. Today, we are going to cover the use of the entire toolbox to get a section done. This can include if/else and more( we’ll get into more detail later). It requires the use of variables and strings that we have already covered. These two are the most important to learn and drive into your brain. They show up everywhere. Learn it, live it, breathe it.
Operators
To start off, we have operators. An operator is the commands that utilize things like arithmetic and Lists. Addition, subtraction, multiplication. The exercise from part 2 covered a basic calculator. This can be considered an arithmetic operator( learn).
For example:
x= 7
y = 29
number = x + y
print(number) = 36
There are more complex strings that use scientific calculator methods using float and integral. Float is used for anything with decimals. There are also boolean operators. This is for a two answer kind of set up. Yes/no, If/then. This is a language of ones and zeroes.
Loops
Loops works with commands in lists of numbers and statements in order to formulate an answer. For example, while count < 5, x= 7. It takes orders from a condition that you set and prints out an answer.
Ex: Code from Learn Python
# Prints out 0,1,2,3,4
count = 0 while count < 5: print(count) count += 1 # This is the same as count = count + 1
After the # is a comment. As you can see the above code went from top to bottom it looked for a condition such as < 5. In addition, you have continue and break loops. Continue and break loops are used in conjunction with for/while loops. They can stop an f(x) and restart it.
Ex: From Learn Python
# Prints out 0,1,2,3,4
count = 0 while True: print(count) count += 1 if count >= 5: break
# Prints out only odd numbers — 1,3,5,7,9 for x in range(10): # Check if x is even if x % 2 == 0: continue print(x)
The above code sets up a while loop for > 5, but prints out only the numbers that are divisible by 2 ( % ).
Then you have else statements that you throw in after a while. This will only be executed if the while statement above it works.
Functions
Functions are a way of organizing and making the code more readable and repeatable.
def my_function()
print(my_funtion())
Arrays
An array holds items or values. Each piece in the array is called an element. Its placement within the array is an index.
Ex: Code from tutorialspoint
from array import *
array1 = array('i', [10,20,30,40,50])
for x in array1:
print(x)The code prints out 10–50 because its is taking from an array above it that choice the number bank. You can also find a specific part of the array.
Ex: tutorialspoint
from array import *
array1 = array('i', [10,20,30,40,50])
print (array1[0])
print (array1[2])The chosen element from the array is inclosed in []I am learning from a really good youtube video here. Its four hours of free coding lessons to get started on python by free code camp. I don’t get anything from this its just a really helpful tool. Learning to code is hard but there are a ton of resources out there to help get us through the process.
References: