Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Python Programming Guide

The document provides a comprehensive guide on using the Pythonista app to learn Python programming through various exercises. It covers fundamental concepts such as printing text, calculations, variables, user input, lists, functions, conditional statements, and loops. Each section includes practical commands to run and encourages users to edit and improve their code.

Uploaded by

jaime
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Programming Guide

The document provides a comprehensive guide on using the Pythonista app to learn Python programming through various exercises. It covers fundamental concepts such as printing text, calculations, variables, user input, lists, functions, conditional statements, and loops. Each section includes practical commands to run and encourages users to edit and improve their code.

Uploaded by

jaime
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

www.simonhaughton.co.

uk

How to use the Pythonista app


to learn the Python
programming language
1. Printing text www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

The print command


print ‘Hello world.’ prints text on screen.
print ‘I can print text.’
print ‘\n’
print ‘Why did the beach cry?’ This prints a blank line.
print ‘Because the seaweed!’

Edit and  Can you change the joke to make it better?


improve: Remember to press to check your program works!

Key vocabulary

Program – A sequence of commands which a computer follows.


Run - Carrying out the commands in a program.
2. Solving calculations www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

print 50 + 50
The answers to these
print 50 – 25
calculations will be printed when
print 50 * 10
the program is run.
print 50 / 5
print (3 * 6) + 2
print (8 + 7) / 3
print (20 – 10) * 5

Edit and  Can you change the numbers in the calculations?


improve: Remember to press to check your program works!

Key vocabulary

Testing - Trying out a program to check if it works as expected.


Debugging - Finding and correcting mistakes in a program's code.
3. Text variables www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

name = ‘Molly’
print ‘Hello’, name You always put text in
This is a variable – inverted commas!
something that can food = ‘chocolate’
change. Print ‘I like to eat’, food

team = ‘Manchester United’


print ‘I support’, team

sport = swimming This prints what the


print sport, ‘is fun’ variable is set as.

 Change the text each Key vocabulary


Edit and
variable is set as. Remember Variable – A value that can be
improve:
the inverted commas! stored and used in a program.
4. Inputting text www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

This is a variable –
The raw_input command makes
something that can
the user type in what they want.
change.

name = raw_input(‘What is your name?’)


print ‘Hello’, name

age = raw_input(‘What is your age?’)


print ‘You are’, age

town = raw_input(‘Where do you live?’)


print ‘You live in’, town

subject = raw_input(‘What subject do you like?’)


print subject, ‘is your favourite’
5. Inputting numbers www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

This is a variable –
something that can The float command tells the
change. computer the user is typing a number.

number = float(raw_input(‘Type a whole number.’))


answer = number * 8
print answer

number2 = float(raw_input(‘Type another whole number.’))


answer = number + number 2
print answer

Edit and
 Change this symbol to do different calculations.
improve:
6. Random numbers www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

This sets a What happens if you change


variable as a the 10 to a smaller number or
random number. the 20 to a bigger number?

import random

number = random.randrange(10,20,1)
print number
print number + 10
print number * 10

Edit and  Change these symbols and numbers to do different


improve: calculations with the random number.
7. Programs with a purpose www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

Variable User input

score = float(raw_input(‘Type your score.’))


This works out total = float(raw_input(‘Type the total possible.’))
what % you got percent = score / total * 100
in a test. print ‘Your percentage is’, percent

width = float(raw_input(‘Type the rectangle width.’))


This works out length = float(raw_input(‘Type the rectangle length.’))
the area of a area = length * width
rectangle. print ‘The area is’, area

Programming challenge:
Create a program that calculates the perimeter of a rectangle by adding
together its two lengths and two widths, inputted by the user.
8. Lists www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

These two variables store lists


import random
of colours and animals.
colours = [‘red’, ’green’] Remember your inverted
animals = [‘lions’, ‘bears’] commas!

print ‘My rainbow zoo has:’


This picks a random colour and
colour = random.choice(colours)
animal and prints it.
animal = random.choice(animals)
print colour, animal Key vocabulary
List – A set of values

Edit and  Put more items in the lists to make the rainbow zoo more fun!
improve:
9. Functions www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

Key vocabulary
Function – A sub-program which can
This function is named cointoss. be called (run) later using its name.

import random
This is a function – a set of
def cointoss():
commands with a name that
options = [‘heads’, ‘tails’] does something (tosses a coin).
result = random.choice(options)
print result
Programming challenge:
cointoss()
cointoss() Change this function to roll a dice
cointoss() instead. Change its name from
cointoss()
cointoss to roll.
cointoss()
Then change the options to
[1, 2, 3, 4, 5, 6]
10. Conditional (if) statements www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

This program prints


‘Correct’ if the user answer = raw_input(‘Do cats bark? ’)
answers correctly, if answer == ‘no’:
REMEMEBR THE
else prints ‘Wrong’ if print ‘Correct’ COLON!
they answer else:
incorrectly.
print ‘Wrong’

== means ‘is the


THIS MUST BE TABBED IN!
same as’

Edit and  Change the question being asked (and the answer too, if needed).
improve:
11. OR statements www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

answer = raw_input(‘Is it dark at night? ’)


if answer == ‘yes’ or answer == ‘YES’:
print ‘Correct’
else:
print ‘Wrong’ The or command lets the user
input different answers but still
get the question correct.

Edit and  Change the question being asked (and the answer too, if needed).
improve:

Key vocabulary
Conditional (IF) statement – Decides which commands to run depending on
whether certain things (conditions) are true or false.
12. Score calculators www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

score = 0
This variable sets the score to
answer = raw_input(‘Is it grass green? ’) 0 at the start.
if answer == ‘yes’ or answer == ‘YES’:
print ‘Correct’
score = score + 1 This adds 1 to the score if the
else: user answers correctly.
print ‘Wrong’

answer = raw_input(‘What is 3 + 3? ’)
The quiz uses conditional (if)
if answer == ‘6’ or answer == ‘six’: statements to print if the user
print ‘Correct’ answers correctly or not.
score = score + 1
else:
Programming challenge:
print ‘Wrong’
Create your own quiz that
print ‘Your score is’, score prints the player’s score at the
end.
13. While loops www.simonhaughton.co.uk

Press and create an . Type in these commands and run them :

password = ‘fish’ This variable sets


guess = ‘’ password as ‘fish’.

while (password != guess): This while loop keeps


guess = raw_input(‘Enter password: ’) repeating while the guess
if password == guess: is wrong, until the guess
print ‘Correct’ is correct.
else:
print ‘Try again’ A conditional (if)
statement prints if the
guess is correct or not.

Key vocabulary
While loop – Commands in a while loop keep repeating until a condition is
met (e.g. the correct password is inputted).

You might also like