PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

71
PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to https ://www.python.org/downloads / Click , download it and install it.

Transcript of PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Page 1: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

PROGRAMMING BASICSProgramming for non-programmers.

You will need Python 2.7.10• Go to https://www.python.org/downloads/ • Click , download it and install it.

Page 2: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Objectives

You will be able to:• Write code! (And read it)• Output and Input: Push and pull data to and from the user• Data Types: Store information in the computer’s memory• Loops: Repeat common operations• Understand how programs are put together

Page 3: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

4 Things To Bear In Mind• Programming is more of a skill than a subject.

• Even if you memorise all the books, your first driving lesson isn’t going to be perfect.

• Reading code is easier if you read right to left.• Think ‘6 = 3 + 3’ rather than ‘3 + 3 = 6’.

• Mistakes are useful.• The more you make, the more you learn.

• I’m going to show you more than you need to know.• It may not seem important, but it will help you find problems later.

Page 4: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Participation• Feedback is useful

• Stop me if you get confused.

• This is not a classroom• If you need to leave, that’s obviously OK.

Page 5: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Natural Language and Formal Language

Question:

How is spoken language different to a programming language?

Check out the next slide...

Page 6: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Natural Language and Formal Language

A uvetrsniiy fnuod taht wodrs wtih mxeid up leettrs cuold slitl

be raed relleivaty esilay.

Hndas up fi yuo cna raed tihs.

Page 7: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Natural Language and Formal Language

A university found that words with mixed up letters could still

be read relatively easily.

Hands up if you can read this.

Page 8: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Natural Language and Formal Language

Question:

How is spoken language different to a programming language?

• Natural language can be structured or unstructured:• “I’m going to the park later; are you coming?”• “Park. Later. Coming?”

• Formal languages, like mathematics and Python have rules. • 3 + 3 = 6 is syntactically correct. 3 3 += 6 is not.• name = ‘Drew’ is syntactically correct. Put Drew in name is not.

• The words and symbols have to be in the right order.

Page 9: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

What can computers do?

Question:

What are the basic building blocks of a computer program?

What can you actually ask computers to do?

Page 10: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

What can computers do?• Compare and manipulate values

• This is done using operators. (+, -, *, /, <, ≤, =, !=, <>, >, ≥, …)

• Store values in memory• Variables hold values and have different types.

• Alter the flow of execution• Loop through something a certain number of times.

• for loops, while loops

• Split the program into paths and control what happens when.• if…else if…else statements, try…catch…finally statements

• Define and run functions• Almost always followed by (brackets).

Page 11: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

First Steps• Open IDLE (Python GUI)

• IDLE stands for Integrated DeveLopment Environment

Page 12: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

print(“Hello, World!”)

Traditionally, the first program written in a new language outputs “Hello, World!” to the screen. So let’s do that.

print(“Hello, World!”)

Questions:

What did that do?

What can we learn from it?

What else can we print()?

Page 13: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

print(“Hello, World!”)

Questions:

What did that do?

Reading right to left:• Set up a string parameter of “Hello, World!”• Passed it to a function called ‘print’• Output the string to the screen

Page 14: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

print(“Hello, World!”)

Questions:

What can we learn from it?

1) Python doesn’t need additional setup.

The equivalent “Hello, World!” in C# is

2) The output function is print()3) Functions can be spotted by their (brackets)

4) We can give functions extra information (parameters) in their brackets

5) Written text needs quotation marks around it

Page 15: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Hidden or visible?• Some lines of code show you something.

• These are expressions.• Could be a value or the result of a calculation.• The value that comes out is called a ‘return value’.• print() outputs text, which is different.

• Other pieces of code just run without showing you anything.• These are statements.• They usually do something useful, but do not need to be shown to the user.

Page 16: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Hidden or visible?

Expressions return a value.

Enter these:• “Drew”• 120• 120 / 60• 15 < 35• min(51, 79, 23, 18, 37)• Try your own!

Statements do not.

Enter these:• name = “Drew”• seconds = 120• minutes = seconds / 60

• Try your own!

Page 17: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Data Types

30 years ago computers had to be told exact data types, including:• Boolean True or false• Date Year, month, day, hour, minutes, seconds, milliseconds (+ zone)• Float Floating point (decimal) numbers• Double Double-precision floating-point numbers• Integer Whole numbers (no decimal places)• Long Very big whole numbers• Char Single characters (either from a keyboard or special characters)• StringOne or more characters put together

Page 18: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Data Types

Python works out the types for you.

Page 19: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Putting It All Together• Just as numbers can be added together to form bigger numbers, strings can

be added to each other to form bigger strings. (This is called concatenation.)

print(“Hello ” + “CodeUp” + “ people”)

name = “Drew”print(“Hello ” + name)

• Remember the spaces – the computer won’t do it for you!

Page 20: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Putting It All Together• Be aware of types when concatenating. • Non-strings must be converted first…

legs = 4stringLegs = str(legs)print(“Most dogs have ” + stringLegs + “ legs.”)

• …though the conversion can be done ‘in-line’.

print(“Most dogs have ” + str(legs) + “ legs.”)

Page 21: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Summary

You have used Python• You wrote your first program! Congratulations!

How computers think• Natural vs formal language• Expressions vs statements• Data types• Functions

• Parameters, return values, (signatures!)

Page 22: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Questions?

Page 23: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Input

So, if print() can get things out of our program…

…how do we get stuff in?

Page 24: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Input

So, if print() can get things out of our program…

…how do we get stuff in?

• Keyboard• Mouse• Files• Internet

Page 25: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Input• Data coming into the program needs to be stored immediately, otherwise the

computer loses track of it.• For this we use variables.

• They hold data in memory so we can access it later.• They have types (int, float, string, etc.)• They have a name.

• Names can’t start with a number or a symbol.

Page 26: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Input• There are two ways of getting data from the user. The first is as raw data.• Raw data is pure, unprocessed text (i.e. string data)• Try it:

name = raw_input(“What is your name? ”)print(name)type(name)

Questions:

What did that do?

What can we learn from it?

Page 27: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

name = raw_input(“What is your name? ”)

Questions:

What did that do?

Reading right to left:• Set up a string parameter of “What is your name? ”• Sent it to a function called ‘raw_input’• Waited for the user to enter data• Assigned the new data a position in memory.• Called that area of memory ‘name’ and typed it as a string variable.

Page 28: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

name = raw_input(“What is your name? ”)

Questions:

What can we learn from it?

Reading right to left:• The string parameter we pass to raw_input() is used as a prompt.• The function returns a value• We can store the returned value• ‘=’ (equals) is the assignment operator• Variables do not have quotation marks around them.

Page 29: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Cooking the books• raw_input() will always return the data entered as a string.• If ‘raw’ is omitted, Python will ‘cook’ the input – work out its type.

age = input(“How old are you? ”)print(age)type(age)

Questions:

What did that do?

What can we learn from it?

Page 30: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

age = input(“How old are you? ”)

Questions:

What did that do?

Reading right to left:• Set up a string parameter of “How old are you? ”• Sent it to a function called ‘input’• Waited for input• Assigned it a position in memory• Worked out what type of value had been entered• Called that area of memory ‘age’ and typed it as an int variable.

Page 31: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Cooking the books• The type of data stored depends on

whether we use raw_input() or input().

• raw_input() always gives us a string type• Even when we type in a number

• input() tries to run what we enter as code• Think of it as ‘cooking’ the raw input

b = input(“true or false? ”)type(b)

Page 32: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Different types do different things

If there are 60 seconds in a minute, what fraction of a minute is 59 seconds?

Try this:

seconds = 59type(seconds)seconds / 60

seconds = 59.0type(seconds)seconds / 60

Page 33: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Different types do different things

Computers are fast, not clever.

Page 34: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Different types do different things

Another way to get around this is by converting the value.

59 will be treated as an integer unless you tell Python different.

float(), int() and str() can all be used to change the type of a value.

Page 35: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Nested functions

Because functions return values, you can use them as parameters.

Page 36: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Summary

You have used functions• print() sends data to the screen• type() tells you the type of data• raw_input() gets data from the user (as a string)• input() gets data and treats it as code, working out the type automatically• float(), int() and str() convert values

There are many data types, used for different purposes• bool (true or false); float (decimal); integer (whole number); string (text)• The type of data affects how the computer processes it

Page 37: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Questions?

Page 38: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Quick Challenge 1

Write code to:• Ask the user their name and store it to a string variable.• Ask the user their age and store it to an int variable.• Tell the person by name how old they will be in ten years.

• Operators: =, + • Functions: raw_input(), input(), print()

Page 39: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Quick Challenge 1

name = raw_input(“What is your name? ”)

age = input(“How old are you? ”)

newAge = age + 10

print(“Hi ” + name + “.”)

print(“In 10 years you will be ” + str(newAge) + “.”)

Page 40: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Loops• Computers can process the same thing thousands of times.• This is called looping and there are two types.

• for loops do things a certain number of timesfor i in range(10): for letter in “Hello”:

print(i) print(letter)

• while loops do things while a condition is truei = 1while i < 10:

i = i + 1print(i)

Page 41: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Loops• Computers can process the same thing thousands of times.• This is called looping and there are two types.

• for loops do things a certain number of timesfor i in range(10): for letter in “Hello”:

print(i) print(letter)

• while loops do things while a condition is truei = 1while i < 10:

i = i + 1print(i)

Note the colons ( : )

Page 42: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Loops• Computers can process the same thing thousands of times.• This is called looping and there are two types.

• for loops do things a certain number of timesfor i in range(10): for letter in “Hello”:

print(i) print(letter)

• while loops do things while a condition is truei = 1while i < 10:

i = i + 1print(i)

The gaps are also very important. This is how

Python knows which lines are part of the loop and

which lines are not.

Page 43: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Loop Control• Loops can also be broken or continued.• For example, if a point in a loop is reached where it’s pointless going any

further, you can call the break statement.

for i in range(10): while i < 10:print(i + 5) print(i + 5)break i += 1

break

Page 44: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Loop Control• Loops can also be broken or continued.• For example, if a point in a loop is reached where it’s pointless going any

further, you can call the break statement.

for i in range(10): while i < 10:print(i + 5) print(i + 5)break i += 1

break

Page 45: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Loop Control• Loops can also be broken or continued.• If you’ve done all you can with a certain value and want to start with the next

one, you can continue.

for i in range(10): while i < 10:print(i + 5) print(i + 5)continue i += 1

continue

Page 46: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Loop Control• Loops can also be broken or continued.• If you’ve done all you can with a certain value and want to start with the next

one, you can continue.

for i in range(10): while i < 10:print(i + 5) print(i + 5)continue i += 1

continue

Page 47: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Quick Break• Lots of languages are developing shortcuts for loops

• Run the following:

n = range(48,127)print nk = [[c,chr(c)] for c in n]print k

Question:• What does it do?

Page 48: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

ASCIIAmericanStandardCode forInformationInterchange

Page 49: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Application of Loops: Complex Series

1, 1, 2, 3, 5, 8, 13, 21, 34...

• The next number is always the sum of the previous two.

Page 50: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Fibonacci

1, 1, 2, 3, 5, 8, 13, 21, 34...

Page 51: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Fibonacci

Page 52: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Fibonacci• This code creates the values in the Fibonacci series:

f = [1, 1]f = f + [ f[ -2 ] + f[ -1 ] ] We’re always taking

two values; the second-to-last and the last.

Page 53: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Fibonacci• This code creates the values in the Fibonacci series:

f = [1, 1]f = f + [ f[ -2 ] + f[ -1 ] ]f = f + [ f[ -2 ] + f[ -1 ] ]

f = f + [ f[ -2 ] + f[ -1 ] ]f = f + [ f[ -2 ] + f[ -1 ] ]f = f + [ f[ -2 ] + f[ -1 ] ]f = f + [ f[ -2 ] + f[ -1 ] ]f = f + [ f[ -2 ] + f[ -1 ] ]f = f + [ f[ -2 ] + f[ -1 ] ]f = f + [ f[ -2 ] + f[ -1 ] ]

The same operation is done over and over.

We may as well loop.

Page 54: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Fibonacci• This code creates the values in the Fibonacci series:

f = [1, 1]

f = f + [ f[ -2 ] + f[ -1 ] ]The same operation is done over and over.

We may as well loop.

Page 55: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Fibonacci• This code creates the values in the Fibonacci series:

f = [1, 1]for i in range(10):f = f + [ f[ -2 ] + f[ -1 ] ]

The same operation is done over and over.

We may as well loop.

Page 56: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Fibonacci• This code creates the values in the Fibonacci series:

f = [1, 1]for i in range(10):

f = f + [ f[ -2 ] + f[ -1 ] ]

Page 57: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Fibonacci• This code creates the values in the Fibonacci series:

f = [1, 1]for i in range(10):

f = f + [ f[ -2 ] + f[ -1 ] ]

This is a statement, so we have to print out the values ourselves.

print(f)or

for n in f:print(n)

Page 58: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Summary

Loops• Useful for repetitive tasks

• Complex series, games, artificial intelligence

• Can be broken or continued• ‘For’ Loops

• Do the same thing a limited amount of times

• ‘While’ Loops• Check a condition each time and loop again if it is true

Page 59: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Questions?

Page 60: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

If…ElseIf…Else: Controlling the Execution Path• Let’s say we’re writing code for a game where you can only turn right.• The user enters a heading to follow.

• If the heading entered is greater than 180, it will take us left.• If the angle is less than 0, the program will break.

• So we have to change what the code does in different situations:• If the angle is less than 0, change it to 0.• If the angle is more than 180, change it to 180.

Page 61: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

If…ElseIf…Else: Controlling the Execution Path

minAngle = 0maxAngle = 180angle = input(“Which way do you want to go? ”)if angle < 0:

angle = minAngleprint(“You can’t go that way.”)

elif angle > 180:angle = maxAngleprint(“You can’t go that way.”)

else:print(“Go go go!”)

Page 62: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

If…ElseIf…Else: Controlling the Execution Path

minAngle = 0maxAngle = 180angle = input(“Which way do you want to go? ”)if angle < 0:

angle = minAngleprint(“You can’t go that way.”)

elif angle > 180:angle = maxAngleprint(“You can’t go that way.”)

else:print(“Go go go!”)

Page 63: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

If…ElseIf…Else: Conditions• Each if/elif/else has associated conditions which must be met in order for that

piece of code to be run.• These can be as short, long or complicated as you want.• Just like in maths, brackets can be used to control the order of importance

user = raw_input(“What’s your name? ”)angle = input(“What’s your heading? ”)

if user == “Drew” and angle < 0:print(“You should know better”)

elif user != “Drew” and (angle < 0 or angle > 180):#Change angle

Page 64: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Summary

If-ElseIf-Else• Used to control which pieces of code are run in different circumstances.• Can have any number of logical checks in any order• Comes in if, if-else, and if-elseif-else flavours.

Page 65: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Questions?

Page 66: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Your Turn• That’s all the learning done.

• As I said at the beginning, the best way to learn it is to do it.• Make mistakes, ask for help.

Page 67: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Challenge

1. Ask the user how many numbers of the Fibonacci set they want.

2. Generate the lines using the code we saw before:

f = [1, 1]for i in range(howManyNumbers):

f = f + [ f[ -2 ] + f[ -1 ] ]3. Print the numbers.

Page 68: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Challenge

howManyNumbers = input(“How many numbers do you want? ”)f = [1, 1]

for i in range(howManyNumbers):f = f + [ f[ -2 ] + f[ -1 ] ]

for number in f:print(f)

Page 69: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

Well Done!

Thank you for coming. I hope you enjoyed yourself.

If you want to try some more:• These slides will be made available online

• There is a book: “Think Python – How to Think Like a Computer Scientist”• Free from http://www.greenteapress.com/thinkpython/thinkpython.pdf

• You can learn for free on Code Academy• They do lots of courses, including one on Python

Page 70: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

The Learning Wheel

• Don’t knowyou know

• Don’t know you don’t know

• Know you don’t know

• Know you know

DKK

DKDK

KDK

KK

Page 71: PROGRAMMING BASICS Programming for non-programmers. You will need Python 2.7.10 Go to

KWDKnow Want to know Don’t know