Difference between revisions of "Basic Programming With Python"

(Continued this article.)
Line 2: Line 2:
  
 
This article will talk about some basic Python programming. If you don't even know how to install python, look [[Getting Started With Python Programming|here]].
 
This article will talk about some basic Python programming. If you don't even know how to install python, look [[Getting Started With Python Programming|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:
 +
 +
<code>
 +
for i in range(1,51):
 +
:print(i)
 +
</code>
 +
 +
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 <math>\sum_{n=1}^{50} 2^{n}.</math>'''
 +
 +
To do this task, we must create a for loop and loop over the integers from 1 to 50 inclusive:
 +
 +
<code>
 +
for i in range(1,51):
 +
</code>
 +
 +
Now what? We must keep a running total and increase it by <math>2^i</math> every time:
 +
 +
<code>
 +
total = 0
 +
 +
for i in range(1,51):
 +
:total += 2**i
 +
</code>
 +
 +
We must not forget to print the total at the end!
 +
 +
<code>
 +
total = 0
 +
 +
for i in range(1,51):
 +
:total += 2**i
 +
print(total)
 +
</code>
 +
 +
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 <math>\boxed{2,251,799,813,685,246.}</math>
 +
 +
===The While Loop===
 +
 +
While loops don't loop over a list. They loop over and over and over...'''until'''...a condition becomes false.
 +
 +
<code>
 +
i = 3
 +
 +
total = 0
 +
 +
while i < 1000:
 +
:total += i
 +
:i += 3
 +
print(total)
 +
</code>
 +
 +
In this code, the while loop loops 333 times, until i becomes greater than or equal to 1000.
 +
 +
====Program Example====
 +
 +
'''Find <math>\sum_{n=1}^{50} 2^n</math> without using a for loop.'''
 +
 +
We must create a while loop that will iterate until n is greater than <math>50.</math>
 +
 +
<code>
 +
n = 1
 +
 +
total = 0
 +
 +
while n <= 50:
 +
:total += 2**n
 +
:n += 1
 +
print(total)
 +
</code>
 +
 +
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, <math>2,251,799,813,685,246.</math>

Revision as of 10:23, 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.


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:

for i in range(1,51):

print(i)

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$ without using a for 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.$