Download presentation
Presentation is loading. Please wait.
1
Programming with Python, for beginners By @GirlGeekUpNorth
2
About Me Taught myself to code Founded a Code Club for Adults
Manager of MadLab STEM Ambassador Digital Skills Facilitator
3
Objectives To be able to read and write code
Understand the basic building blocks of a program Display information to users (output) Name different data types Store data provided by users (input) Collect data from the user and display it in a way that you choose
4
Some Things to Consider
Programming is a skill – it takes practice, and you need to expect to make mistakes Reading code is easier when done right to left Think 6 = instead of = 6 Mistakes are useful – they help you learn
5
Get Involved! This is not a classroom, if you need to leave then that’s ok Ask questions Say if you’re confused or we’re going too fast
6
Natural Language vs Formal Language
Question: How is spoken language different to a programming language? Check out the next slide...
7
Can you read this? A uvetrsniiy fnuod taht wodrs wtih mxeid up leettrs cuold slitl be raed relleivaty esilay. Hndas up if you can raed tihs.
8
Natural Language vs Formal Language
How is spoken language different to 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 = 6 is not. The words and symbols have to be in the right order
9
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).
10
Trinket.io Can be used to program with Python without worrying about installing software or using the terminal
11
“Hello, World” print (“Hello, World”) Questions: What did that do? What can we learn from it? What else can we print()?
12
“Hello, World” What did we 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
13
“Hello, World” What did we learn from it? 1) Python doesn’t need additional setup. 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
14
“Hello, World” What else can we print? print (“MadLab”) print (27) print (3.5) print (“Once upon a time…..”)
15
Data Types Text is referred to as a “string” Numbers can be integers (whole numbers) Or floating point numbers (decimals) Booleans are True or False string “hello world” integer 5 float 2.7 boolean True
16
Data Types Python can work out the data type for you Try entering into the console print type() What different data types can you find?
17
Data Types We can find out how values are treated using the ‘type’ function. print type(“Hello, World!”) print type(‘Hello, World!’) print type(17) print type(3.2) print type('17') print type('3.2') print type( ) print type(1,000,000) This will cause an error because commas are special
18
Operators Operators allow us to manipulate variables +, -, *, /, <, >, ==, % print (6 / 3) print (5 + 7) print (10 % 6) print (4 * 5)
19
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 ” + “coding” + “ people”) Remember the spaces – the computer won’t do it for you!
20
Inputs So if the print() function allows us to output our data, how do we get the data in? 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.
21
Variables Variables hold data in memory so we can access it later. They have types (int, float, string, etc.) They have a name. variable_name = variable Example my_name = “Claire” my_age = 21
22
Variable Types name = “Claire” age = 32 town = “Salford” print name print type(name)
23
Converting Data Types Define your variables legs = 4 pet = “Dog” name = “Charlie” Concatenate your variables into this sentence print (“My “ + pet + “ called “ + name + “ has “ + legs + “ legs”)
24
Converting Data Types legs = 4 pet = “Dog” name = “Charlie” legs = str(legs) print “My “ + pet + “ called “ + name + “ has “ + legs + “ legs” string = str, integer = int, float = float
25
Raw Input How about we look at how a program can request information? Raw Input is a form of asking for data name = raw_input(“What is your name?”) print (name) print type(name)
26
Raw Input 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
27
Raw Input 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.
28
Raw Input & Data Types As a default, raw_input returns the data as a string If we want the data to be treated as something else, we must pre-define this age= raw_input(“How old are you”) print type(age)
29
Converting Raw Inputs age = int(raw_input(“How old are you?”)) price = float(raw_input(“What does it cost?”)) print type(age) print type(price)
30
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) float(), int() and str() convert values
31
Challenge! Ask the user for their name, store this as a string
Ask the user their age, store this as an integer Convert the user’s age into a string Concatenate the name and age into this sentence; The user is called name and they are age years old Print the result of each step to the console
32
Functions We have used built-in functions: print(“Hello, World!”) type(text) raw_input(“Type something. ”)
33
Whitespace Whitespace is used to structure code, and must be used correctly Indentations are used to separate blocks of code One indentation is either one stroke of the tab key, or four strokes of the space bar Indent your code to show it belongs to the code above
34
Defining Functions We can use Python to define our own functions def addition(x,y): return x + y print addition(4,6)
35
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 times for i in range(10): print (i) for letter in “Hello”: print(letter)
36
Break it down for i in range(10): print (i) print range(10) ‘i’ represents the iteration through the range, the loop function simply moves ‘i’ along the list
37
Loops Computers can process the same thing thousands of times
This is called looping and there are two types while loops do things while a condition is True i = 1 while i < 10: i = i + 1 print(i)
38
Break it down i = 1 while i < 10: i = i + 1 print(i) ‘i’ is set to 1, each time the loop is run, the value of ‘i’ is increased by 1
39
Summary Loops Useful for repetitive tasks Can be broken or continued
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
40
If/Else Statements What if we wanted to change the outcome of our function, depending on what information we feed in? def bythree(number): if number % 3 == 0: return “divisible by three” else: return “not divisible by three”
41
If/Else Statements def bythree(number): if number % 3 == 0: return “divisible by three” else: return “not divisible by three” print bythree(9) print bythree(7)
42
Elif If more than two conditions need to be evaluated, we use elif for additional parameters def bythree(number): if number % 3 == 0: return “divisible by three” elif number == 5: return “you entered 5!” else: return “not divisible by three”
43
Challenge! Given two integer values, return their sum. Unless the two values are the same, then return double their sum. sum_double(1, 2) → 3 sum_double(3, 2) → 5 sum_double(2, 2) → 8 def sum_double(a, b):
44
What Next? Codecademy – Python Track
Coursera – Programming for Everyone CodeUp Manchester
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.