Difference between revisions of "Basic Programming With Python"

(Added Understanding Functions)
m
Line 236: Line 236:
  
 
Flow consists of if statements, elif (else if) statements, and else statements.
 
Flow consists of if statements, elif (else if) statements, and else statements.
 +
 +
An if statement checks if a comparison is true.
 +
 +
An else statement checks if a comparison is not true. It is used after an if statement (and all elif statements after the if statement) and it shares the same comparison as the if statement.
 +
 +
An elif statement checks for two things: if a comparison is true, and if a comparison is not true. It is used right after the if statement.
  
 
===Program Example===
 
===Program Example===
Line 293: Line 299:
  
  
True and False are what are called '''booleans'''.
+
True and False are what are called '''booleans'''. When an if or elif statement receives True, the code inside of the statement happens. When an if or elif statement receives False, the code inside of the statement does not happen. 
  
Back to our program. We must create a for loop that will iterate through all two digit positive integers.  
+
We must create a for loop that will iterate through all two digit positive integers.  
  
  

Revision as of 17:16, 10 March 2018

Important: It is extremely recommended that you read Getting Started With Python Programming before reading this unless you already know some programming knowledge.

This article will talk about some basic Python programming. If you don't even know how to install python, look here.


Loops

There are two different kinds of loops in Python: the for loop and the while loop.

The For Loop

The for loop iterates over a list, or an array, of objects. You have probably seen this code before:

Capture.PNG

This for loop iterates over the list of integers from 1 to 51, excluding the 51 and including the 1. That means it is a list from 1 to 50, inclusive. On every iteration, Python will print the number that the loop is iterating through.

For example, in the first iteration, i = 1, so Python prints 1.

In the second iteration, i = 2, so Python prints 2.

This continues so on until the number, 50, is reached. Therefore, the last number Python will print out is 50.

Program Example

Find $\sum_{n=1}^{50} 2^{n}.$

To do this task, we must create a for loop and loop over the integers from 1 to 50 inclusive:


for i in range(1,51):


Now what? We must keep a running total and increase it by $2^i$ every time:


total = 0

for i in range(1,51):

total += 2**i


We must not forget to print the total at the end!


total = 0

for i in range(1,51):

total += 2**i

print(total)


You must exit out of the for loop one you reach the print(total) line by pressing backspace.

Once you run your program, you should get an answer of $\boxed{2,251,799,813,685,246.}$

The While Loop

While loops don't loop over a list. They loop over and over and over...until...a condition becomes false.


i = 3

total = 0

while i < 1000:

total += i
i += 3

print(total)


In this code, the while loop loops 333 times, until i becomes greater than or equal to 1000.

Program Example

Find $\sum_{n=1}^{50} 2^n$ using a while loop.

We must create a while loop that will iterate until n is greater than $50.$


n = 1

total = 0

while n <= 50:

total += 2**n
n += 1

print(total)


We must not forget to include the n += 1 line at the end of the while loop!

If we run this, we will get the same answer as last time, $2,251,799,813,685,246.$

Functions

In this section, we will define new operations and do arithmetic with them in Python.

The New Operation

Let's say that you defined a new binary operation, the $\uparrow \downarrow.$ You want it to be so $a \uparrow \downarrow b = a^{b}b^{a}.$ Therefore, $1\uparrow \downarrow 2 = 1^{2}2^{1}=1\cdot 2= 2,$ and $2\uparrow \downarrow 3 = 2^33^2 = 8\cdot 9 = 72.$ Let's call this operation an up down arrow.

Program Example 1

Find $[2 \uparrow \downarrow (1 \uparrow \downarrow 2)] \uparrow \downarrow 2.$

We know this will be a big number, so we should write a program to do it! We certainly don't want to do the exponentiation and multiplication every time we use the operation in our program, and that's where functions come in! Functions can take in parameters ($a$ and $b$) and return a result depending on the parameters.


def up_down_arrow(a,b):

return ((a**b) * (b**a))


Above is the code to define our function. We need to print the final result at the end, so we must put a print statement at the end.


def up_down_arrow(a,b):

return ((a**b) * (b**a))

print(up_down_arrow(up_down_arrow(2,up_down_arrow(1,2)),2))


All those nested up_down_arrow's might be confusing at first, but it really isn't that confusing. If you run your program, you should get your answer: $16,777,216.$

You might be surprised that the answer is so big. If you actually start calculating the real answer without a program, you will really quickly find that the final result is $16 \uparrow \downarrow 2,$ which does turn out to be 16,777,216 if you use a calculator.

Let's make another operation!

We will define the operation $\uparrow \downarrow \leftarrow \rightarrow$ to be called all around. $a \uparrow \downarrow \leftarrow \rightarrow b = (ab(a \uparrow \downarrow b))^2.$ That's an operation that will make really big numbers!

Program Example 2

Find 87 all around 132.

The answer is bound to be a huge number, so we must make a program to solve it.

We will keep our up_down_arrow function because in the definition of our all_around function we will use it. Then, we will define the all_around function.


def up_down_arrow(a,b):

return ((a**b) * (b**a))

def all_around(a,b):

return ((a * b * up_down_arrow(a,b)) ** 2)


We must print all_around(87,132) at the end. Once we do that, and we run our program, we get a number so huge that if you have a low end PC, your Python might give a MemoryError. If that happens, don't worry about it.

Here's the huge number:

13587297631464779828575459193700511069135921009091375432890427471255548348541111505004724234140958867581538 4544700095575139880300450300332667952300124312659454107787172869140994734083903393051401716252405338977923 676705927084707669679384028974201372990703503147805423115466316373869490975200336396233735355502238744775 9891969359740267436823768225701073970877427245902169001057267419452765441119054450438245054272985286180533 797417127792755022040372823510595936398640365376868040870762323395254152264821642444071107668433903396506 289607562793460087999004181940101666490177708306438875083164991066701839035438073287566335639223504397929 4341625047852720259316143744829000868294255892155754941579915306846855602120470899115742390753069716079536 554155840959042366798572487573335616432239739041473401428518140909461479933849445336033056039568470375624 172123108054962032236304381550477478287376384.

That number has over 800 digits!

Understanding Functions

Every single function has a return value. If a function does not return anything, the return value is null. Null is a term used in programming as a placeholder that stands for nothing.

Define a function that adds 1 to an input.

How will the function know what number to add 1 to? We will input a parameter for the function to add 1 to.


def add_one(x):

return x + 1


In this function, $x$ is the parameter.


def add_one(x):

return x + 1

print(add_one(2))


In the print statement, we set $x$ as 2, and the function returns $2 + 1 = 3,$ so the program prints $3$.

Define a function that prints out an input.

We will use a parameter again.


def print_function(x):

print(x)


Uh oh! We have nothing to return! Therefore, we will return nothing (or null), by just writing return.


def print_function(x):

print(x)
return

print_function("print_function")

This code prints out print_function. print_function() is said to be called in the final line of the code. Again, we set $x$, our parameter, as "print_function."

Flow

Flow consists of if statements, elif (else if) statements, and else statements.

An if statement checks if a comparison is true.

An else statement checks if a comparison is not true. It is used after an if statement (and all elif statements after the if statement) and it shares the same comparison as the if statement.

An elif statement checks for two things: if a comparison is true, and if a comparison is not true. It is used right after the if statement.

Program Example

Does the number 10 have the property that when you multiply 10 by 5, you get a two digit number?

This problem is super easy to solve without a program, but let's write a program to solve this anyway. We will use an if statement to check if $10\cdot 5$ is less than 100 and greater than 9.


if 10*5 > 9:

if 10*5 < 100:
print("Yes")
else:
print("No")

else:

print("No")


This code means, if 10*5 > 9, then we will check if it is less than 100. If it is not greater than 9, however, we will print No. If it is greater than 9, we check if it is less than 100. If it is, we print Yes. If it isn't we print No.

These nested if statements can be very confusing. Luckily, there is a faster an easier way to do this.


if 10*5 > 9 and 10*5 < 100:

print("Yes")

else:

print("No")


This code checks for the two conditions at the same time. If we run it, we get our answer of Yes.

Booleans

Program Example

Print all two digit positive integers $x$ such that $5x$ is a two digit positive integer.

We can create a function with our previous code.

We will have to slightly modify our function so we can use it in a for loop at the end:


def check(a):

if a*5 > 9 and a*5 < 100:
return True
else:
return False


True and False are what are called booleans. When an if or elif statement receives True, the code inside of the statement happens. When an if or elif statement receives False, the code inside of the statement does not happen.

We must create a for loop that will iterate through all two digit positive integers.


def check(a):

if a*5 > 9 and a*5 < 100:
return True
else:
return False

for i in range(10,100):

if check(i):
print(i)


The final for loop checks if $5i$ is a two digit integer. If it is, the function returns True and the code inside the if statement gets run. If it isn't, the function returns False and the code inside the if statement gets ignored.

If we run this, we will get the answer.

All two digit integers from 10 to 19 inclusive work!