Difference between revisions of "Basic Programming With Python"

m (Functions)
m (Program Example)
Line 78: Line 78:
====Program Example====
====Program Example====
'''Find <math>\sum_{n=1}^{50} 2^n</math> without using a for loop.'''
'''Find <math>\sum_{n=1}^{50} 2^n</math> using a while loop.'''
We must create a while loop that will iterate until n is greater than <math>50.</math>
We must create a while loop that will iterate until n is greater than <math>50.</math>

Revision as of 19:27, 8 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.


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:

for i in range(1,51):


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


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


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


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.$


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))


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!

Invalid username
Login to AoPS