0% found this document useful (0 votes)
56 views7 pages

Python FlashCards

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views7 pages

Python FlashCards

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Flashcards @biglesp Python Flashcards @biglesp

1: Output
print "hello world"
Python Programming
For Python 2.7 name = "fred"
Les Pounder print name
v1.0
print "hello " + name + " how are you?"

We can join strings together, this is called


concatenation.
2: Comments 3: Variables (Of Type String)
#Comments start with a hash and #comment out one firstname = "fred"
line at a time. surname = "jones"
fullname = firstname + " " + surname
“”” greeting = "hello "
You can block out lots of lines using three “ greeting += fullname
symbols at the start and end of the block. print greeting
“””

Variables are boxes that can contain anything, we


Challenge: Remember to add comments to your code! name them so we can use them.
Python Flashcards @biglesp Python Flashcards @biglesp
Python Flashcards @biglesp Python Flashcards @biglesp
4: Numbers 5: Variables (Of Type Number)
1+1 secsInMin = 60
1.0 + 1.0 minsInHr = 60
hrsInDay = 24
Int() secsInDay = secsInMin * minsInHr * hrsInDay
float() print secsInDay

Variables can also store float numbers.

Challenge: create two variables one containing a


Challenge: Find a sum that you know is correct, string, another an integer. Try and join them
but Python will get it wrong. together.
6: Arithmetic 7: User Input
a=1.3 name = raw_input("what is your name?")
b=2.7 print "hello " + name
c=a+b
print c age = int(raw_input("how old are you?"))
c=a-b print "next year you will be " + str(age+1)
print c
c=a*b Any input is stored as a string!
print c
c=a/b
print c
Challenge: Write some code that asks for two
Challenge: Write some code that converts £ to $ numbers, then adds them together and prints the
result.
Python Flashcards @biglesp Python Flashcards @biglesp
Python Flashcards @biglesp Python Flashcards @biglesp
8: If 9: Conditions
age=10 a=1
if age > 16: if a==1:
print "you have left school" print "equal"
if a!=1:
print "not equal"
if a<1:
print "less"
if a>1:
print "greater"
if a<=1:
print "less or equal"
if a>=1:
Challenge: Add more conditions to the above code, print "greater or equal"
hint use Card 9 to learn more. Try changing the
variable and see what the outcome is.
10: If...Else 11: If...Elif...Else
age=10 age=10
if age>16: if age<5:
print "you can drive a car" print "you are at nursery"
else: elif age<7:
print "you are not old enough" print "you are at infants"
elif age<10:
print "you are at juniors"
elif age<17:
print "you are at secondary school"
else:
print "you have left school"
Challenge: Use raw_input (Card 7) to ask the user
their age, store it as a variable and then
evaluate it using the above code.
Python Flashcards @biglesp Python Flashcards @biglesp
Python Flashcards @biglesp Python Flashcards @biglesp
12: And...Or 13: For
a=1 name="fred"
b=2 for ch in name:
if a>0 and b>0: print ch
print "both are non zero"
total=20
if a>0 or b>0: for n in range(total):
print "at least one of them is non zero" print n

for n in range(1,20):
print n

for n in range(1,20,2):
Challenge: Write a script that asks the user for print n
their favourite food and then drink, which are
stored as variables, then alter the code above to
reflect this. Hint use == to compare.
14: While 15: Functions (No Parameters)
while True: def myname():
print “Hello” print "my name is fred"
print “World”
myname()
Tip: Use Control + C (or Command + C for Mac) to
stop the program.

Challenge: Write a loop that prints “Hello” on


one line, then your name, saved in a variable Challenge: Write a function that asks the user
called name. for their name and then prints it 20 times.
Python Flashcards @biglesp Python Flashcards @biglesp
Python Flashcards @biglesp Python Flashcards @biglesp
16: Functions (With Parameters) 17: Functions (With Return)
def showname(name): def square(n):
print "my name is " + name return n*n

def info(name, age): print square(5)


print "my name is " + name print square(10)
print "my age is " + str(age) a=100
print square(a)
showname("fred") print square(a+10)
showname("harry") b=square(a)
print b
info("fred", 10)
info("harry”, 20)

Challenge: Refer to Card 6 and create a function


that converts $ to £. Where n is the valune in $.
18: Boolean (True / False) Logic 19: Import Modules / Libraries
10 == 10 import time
10 > 20
10 < 20 time.sleep(2)
10 != 11

Challenge: Improve Card 14 so that there is a


Challenge: Use this logic to expand on Card 11. delay between each print statement.
Python Flashcards @biglesp Python Flashcards @biglesp
Python Flashcards @biglesp Python Flashcards @biglesp
20: Turtles 21: Lists

from turtle import * list = [“Banana”]


forward() Add to List
right() list.append(“Apple”)
left() print list
color(“”) print list[0]
circle(r)
penup() Remove from list
pendown() list.pop(x) where x is the position of the item
reset() that you wish to remove.

Challenge: Write a loop that will draw 20 circles


starting with a radius of 20 and ending after 20 Challenge: Create a list to store your shopping,
loops. and then print out each item on a new line.
22: Breaking Out Of A Loop 23: Random

while True: import random


answer = raw_input(“Press x to exit”)
if answer == “x” print random.choice[list]
break print random.randint(1,100)

Challenge: Use random.choice to select a fruit


that children should eat from the list in Card
Challenge: Create a loop that prints the players 21, and add it to a sentence (Cards 1 & 3 will
name 20 times, then breaks. help)
Python Flashcards @biglesp Python Flashcards @biglesp

You might also like