Python Bites

Python Bites
For Python 3.x
Output
These statements output text to the interpreter
print(‘Hello World’)
print(1)
print()
print(1+1)
How it appears
in the IDE pyscripter
http://portablepython.com/
Click Green arrow to run
Variables

Named areas for values

Assignment – putting a value into a variable
x=2
y=3
z=x+y
print(‘x, y, z’)
print(x, y, z)
Input
myName = input(‘Enter Your Name’)
x = int(input(‘Enter an integer ‘)
y = float(input(‘Enter a real number ))
Datatypes of variables

Integer – counting / whole number

Real (float) – real number, can have something after the decimal point

Character – single letter/digit/symbol

String – one or more characters

Boolean – True or False

Python decides datatype of a variable based on what you put into it

Other languages make you decide and declare types e.g. C#

In python, character is not separate from string
Strings


Valid strings

Andy Diament

£$%

12345

03/09/2014

10:44 am
Concatenate – join strings together
first = ‘Andy’
last = ‘Diament’
name = first + last
Basic Maths

+ add

- subtract

* multiply

/ divide
y=8/3
# answer is 2.666666

// Integer divide
y = 8 // 3
#answer is 2

% mod – remainder after dividing
y=8%3
# answer = 2

** raise to a power e.g. y = x**2
If Statement – selection
Test if a condition is true
if x==2:
if x==2:
# if true
print(‘x is 2’)
print(‘x is 2’
print()
print()
elif x == 3:
# else if
print(‘x is 3’)
if x==2:
# if true
print(‘x is 2’)
print()
else:
# if false
print(‘x is not 2’)
print()
else:
print(‘x is not 2 or 3’)
Comparison operators
==
Equal 2
myName == ‘Andy’
!=
Not equal 2
X != y
>
Greater than
>=
Greater than or equal to
<
Less than
<=
Less than or equal to
For loop

Allows you to repeat

E.g. 5 x table
for counter in range(1,13):
sum = counter * 5
Repeats a block of code
The variable (counter) has it’s value changed from 1 to 12
Each time, the code is run with the increased value of
counter
for i in range(12):
# executes 12 times, setting i to 0, 1, 2…11
print (counter ‘ x 5 = ‘, sum)
Typical python thing to repeat up to 1 less than the second
parameter
While loop

Repeats whilst a condition is true
password = ‘sesame’
guess = ‘’
while guess != password:
guess = input (‘Enter a password ‘)
print(‘Correct password’)
Lists

These are data structures that contain a list of numbers, strings etc

Each item has a ‘cell reference’ – a number for the position of the item,
starting from 0

aList = [1,2,3,4,5]
#makes a string in square brackets

print(aList[0])
# prints 1

print(aList[3])
# prints 4

print(aList[5])
# prints error message as the list is not long enough
Functions and procedures

Separate blocks of code - subprograms

Each has a name

You ‘call’ a function to pass control to it

You then return to the calling function
Functions
def add(x, y):
z=x+y
return z
def main():
a=2
b=3
c = add(a,b)
• Starts with main()
• Gets to c = add(a,b)
• a and b are parameters – values passed to the
function
• These are put into x and y in def add(x,y):
• Executes the statements in add
• z is the return value – that is calculated
• Control returns to main
• The value of z is put into c
Random numbers –external functions
import random
# uses an external module
def main():
print(random.randint(1,10)) #random integer between 1 and 10 inclusive
low = 1
hi = 100
a = random.randint(low, high)
Print(random.random())
# random between 0 and 1
Procedures
Like functions, don’t return a value (still called functions in python)
def showGreater(x, y):
if x > y:
print(x, ‘ is greater than ‘, y)
elif y>x:
print(y, ‘ is greater than ‘, x)
else:
print(x,’ is equal to ‘, y)
def main():
a=2
b=3
showGreater(a,b)
Write to file
def main():
outFile = open('data.txt','w')
#open logical file called outfile
#data.txt is name of file
#'w' means write mode - write or overwrite
outFile.write('Forename, Surname\n')
#write 3 lines
#\n means make a new line
outFile.write('Andy, Diament\n')
outFile.write('Dan, French\n')
outFile.close()
#close the file
Read from file
def main()
infile = open('data.txt','r')
for counter in range(3):
input = infile.readline()
line = input.strip()
print(line)
infile.close()
# open for reading
# loop through 3 lines
#read a line
# remove invisible characters e.g. \n
# print the line
# close the file