Code First Girls

?

Function

A reusable piece of code that completes a specific task

You can recognise a function as they are a word followed by round bracks e.g. print ( )

The print ( ) function is used to output a message to the programmer 

You can change the data given to the funtion to change the output 

1 of 10

Numbers and Operators

Integer : a Pyhton data type for whole numbers e.g. 5, 1048, -99

Float : a Phyton data type for decimal numbers e.g. 5.6, 6.0, -67.101

2 of 10

Operator Types

+ add

- subtract

* multiply

/ division

** exponent

% modulo (remainder)

3 of 10

Python Console

There are two main ways to write and run Python programs:

1. With files

2. On the python console (also called the shell)

Python File

  • runs all lines from top to bottom
  • only shows output when using print ( )
  • for code that will be ran multiple times

Python Console

  • runs one line as it is entered
  • shows output for every line
  • interactive for exploration
4 of 10

The String Data Type

String : a python data type for text and characters

e.g. 'Hello' , 'abcde12345' and 'cats' are all strings

Strings must be written between a pair of single or double speech marks

'...' or "..."

Forgetting the speech marks will cause this error 

 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined


The * and + operators work on strings as well as integers
5 of 10

* and + operators

1. The + operator can join two strings together, this is called concatenation

2. The * operator repeats a string a number of times

3. .upper( ) , .lower( ) and .title( ) are methods

6 of 10

Method

method : A repeatable piece of code that completes a task for a specific data type

methods are like functions, but they are tied to a specific data type. e.g. .upper( ) can only be used with a string and not a integer or float

Running this code 

print("Cat" + 3)

Will cause this error....

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str


Putting a number in str( ) converts it to a string:
print("Cat" + str(3))
7 of 10

Variables

variable : a reusable label for a data value in Python. Creating (assigning) a variable has three parts:

1. The variable's name

2. An equals sign = 

3. The data value it references

e.g. username = 'sarah_1987'

age = 23

Values and variables are interchangeable. A variable can be put anywhere that a data value can be used

print( 'spaghetti' )

food = ' spaghetti ' print(food)

8 of 10

String Formatting

Python strings have a method (.format ( ) ) that substitues place-holders { } for values

oranges = 12
cost_per_orange = 0.5

total_cost = oranges cost_per_orange
output
= "{} oranges costs £{}".format(oranges, total_cost)

print(output)

This could have been written as:

oranges = 12
cost_per_orange = 0.5

total_cost = oranges  cost_per_orange
output = str(oranges) + " oranges costs £" + str(total_cost)
print(output)

9 of 10

Comments

comment : a way for a programmer to write a human-readable notes in their code. When running a program, comments are ignored by Python

# This is a comment


e.g.
# A program to calculate the cost of some oranges

oranges = 12
cost_per_orange = 0.5

total_cost = oranges * cost_per_orange
output = "{} oranges costs £{}".format(oranges, total_cost)

print(output) 

10 of 10

Comments

No comments have yet been made

Similar Computing resources:

See all Computing resources »See all Data Types and Variables resources »