(GCSE ARCHIVE) Python Concepts & Syntax Flashcards
Statement
A single instruction or step within a program
Record
A data structure that stores elements, as a series of attributes, used in databases.
Variable
A data element in a program whose value can change as the program is running
Constant
A data element in a program whose value does not change as the program is running
Input
Data introduced into a computer system from the outside world
Output
Data that is presented to the outside world, such as by a printer or display
Function
A named set of instructions that returns a value
Procedure
A named set of instructions that does not return a value
Assignment
The process of allocating a value to a data element
String
A data type capable of storing combinations of numbers, letters and symbols
Iteration
A type of programming construct in which a group of statements is executed repeatedly
Selection
Program construct in which a condition is used to decide which instructions (if any) will be executed
Sequence
Program construct in which a set of instructions are executed in the order in which they appear
Casting
The process of converting one data type into another data type
How to assign a string, integer, float, and Boolean in python
- bob = “string”
- john = 19
- david = 3.14
- sam = True
How to convert a variable into a string, integer or float for concatenation. Rules for each one.
str(variable)
int(variable)
float(variable)
- String works for any data type
- Int only works integer strings (“12”) and truncates floats (12.8 becomes 12).
- Float only for integers and whole number string (“12” or 12 becomes 12.0)
How to make a user input a string, integer, float
- string = input(“Enter a string:”)
- integer = int(input(“Enter an integer”))
- float = float(input(“Enter a float”))
Arithmetic operators in python for addition, subtraction, multiplication, division, integer division, remainder.
Addition) +
Subtraction) -
Multiplication) *
Division) /
Integer Division) //
Remainder) %
Boolean operators in python, not equal to, less than or equal to, and greater than or equal to.
Not equal to) !=
Less than or equal to) <=
Greater than or equal to) >=
Return the number of characters in a string
- len(string)
- counting starts starts at 1
name = “Faisal”
print(len(name))
6
How to return the position of the first occurennce of a particular value in a string.
- string.index(substring)
- starts counting from 0, as it is an index
string = “Faisal”
print(string.index(“F”))
0
How to extract a character at a certain position in a string
- string[x]
- starts counting from 0, as it is an index
string = “Faisal”
print(string[0])
F
How to make a string all uppercase
- string.upper()
string = “Faisal”
print(string.upper())
FAISAL
How to make a string all lowercase
- string.lower()
string = “Faisal”
print(string.lower())
faisal
How to extract a substring from one point to another
- string[x:y]
- be careful, as this starts counting from 1, unlike regular index.
- you could subtract (y-x) to get length
string = “Faisal”
print(string[3:6])
sal
How to check if a string contains only digits
- string.isdigit()
age = 8
print(age.isdigit())
True
How does string concatenation work? Example.
name1 = “Faisal”
name2 = “Mohammad”
print(name1+ name2)
FaisalMohammad
print(name1, name2)
Faisal Mohammad
How do lists work in python?
- They work the same way as arrays. This is a 2D array list:
vowels = [“a”, “e”, “i”, “o”, “u”]
print(vowels[0])
print(len(vowels))
a
5
How to make 2D lists/arrays in python?
listTwo = [[1, 2, 3], [4,5,6]]
print(listTwo[1])
print(listTwo[0][1])
[4, 5, 6]
2