Code First Girls
- Created by: Georgiapettman
- Created on: 06-05-20 14:42
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
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
Operator Types
+ add
- subtract
* multiply
/ division
** exponent
% modulo (remainder)
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
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
* 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
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))
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)
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)
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)
Related discussions on The Student Room
- quick matlab question »
- How to write code »
- iDEA Silver Citizen Badge 2 »
- GCSE Computer Science »
- Silver idea award »
- OCR GCSE Computer Science Paper 2 (J277/02) - 25th May 2023 [Exam Chat] »
- Silver iDEA Citizen badge part two »
- Edexcel A-level Statistics Paper 2 (9ST0 02) - 12th June 2023 [Exam Chat] »
- Stuck on Idea Silver badge Maker Activation »
- silver idea digital citizen part three »
Comments
No comments have yet been made