Difference between revisions of "Basic Programming With Python"

m (The For Loop: Fixing some tiny weird things.)
m (Put some extra spaces for good formatting.)
Line 65: Line 65:
  
 
While loops don't loop over a list. They loop over and over and over...'''until'''...a condition becomes false.
 
While loops don't loop over a list. They loop over and over and over...'''until'''...a condition becomes false.
 +
  
 
<code>
 
<code>
Line 76: Line 77:
 
print(total)
 
print(total)
 
</code>
 
</code>
 +
  
 
In this code, the while loop loops 333 times, until i becomes greater than or equal to 1000.
 
In this code, the while loop loops 333 times, until i becomes greater than or equal to 1000.
Line 84: Line 86:
  
 
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>
 +
  
 
<code>
 
<code>
Line 95: Line 98:
 
print(total)
 
print(total)
 
</code>
 
</code>
 +
  
 
We must not forget to include the n += 1 line at the end of the while loop!  
 
We must not forget to include the n += 1 line at the end of the while loop!  
Line 112: Line 116:
  
 
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 (<math>a</math> and <math>b</math>) and return a result depending on the parameters.
 
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 (<math>a</math> and <math>b</math>) and return a result depending on the parameters.
 +
  
 
<code>
 
<code>
Line 118: Line 123:
 
:return ((a**b) * (b**a))
 
:return ((a**b) * (b**a))
 
</code>
 
</code>
 +
  
 
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.
 
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.
 +
  
 
<code>
 
<code>
Line 127: Line 134:
 
print(up_down_arrow(up_down_arrow(2,up_down_arrow(1,2)),2))
 
print(up_down_arrow(up_down_arrow(2,up_down_arrow(1,2)),2))
 
</code>
 
</code>
 +
  
 
All those nested up_down_arrow's might be confusing at first, but it really isn't that confusing.
 
All those nested up_down_arrow's might be confusing at first, but it really isn't that confusing.
Line 144: Line 152:
  
 
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.
 
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.
 +
  
 
<code>
 
<code>
Line 152: Line 161:
 
:return ((a * b * up_down_arrow(a,b)) ** 2)
 
:return ((a * b * up_down_arrow(a,b)) ** 2)
 
</code>
 
</code>
 +
  
 
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.'''
 
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.'''
Line 178: Line 188:
  
 
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 <math>10\cdot 5</math> is less than 100 and greater than 9.
 
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 <math>10\cdot 5</math> is less than 100 and greater than 9.
 +
  
 
<code>
 
<code>
Line 189: Line 200:
 
:print("No")
 
:print("No")
 
</code>
 
</code>
 +
  
 
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.
 
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.
 
These nested if statements can be very confusing. Luckily, there is a faster an easier way to do this.
 +
  
 
<code>
 
<code>
Line 201: Line 214:
 
:print("No")
 
:print("No")
 
</code>
 
</code>
 +
  
 
This code checks for the two conditions at the same time. If we run it, we get our answer of Yes.
 
This code checks for the two conditions at the same time. If we run it, we get our answer of Yes.
Line 213: Line 227:
  
 
We will have to slightly modify our function so we can use it in a for loop at the end:
 
We will have to slightly modify our function so we can use it in a for loop at the end:
 +
  
 
<code>
 
<code>
Line 222: Line 237:
 
::return False
 
::return False
 
</code>
 
</code>
 +
  
 
True and False are what are called '''booleans'''. True and False are actually the only booleans!
 
True and False are what are called '''booleans'''. True and False are actually the only booleans!
  
 
Back to our program. We must create a for loop that will iterate through all two digit positive integers.  
 
Back to our program. We must create a for loop that will iterate through all two digit positive integers.  
 +
  
 
<code>
 
<code>
Line 238: Line 255:
 
::print(i)
 
::print(i)
 
</code>
 
</code>
 +
  
 
The final for loop checks if <math>5i</math> 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.
 
The final for loop checks if <math>5i</math> 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.

Revision as of 18:45, 9 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!

Flow

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

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. True and False are actually the only booleans!

Back to our program. 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!