Gallery
Solution Class List
Share
Explore
Solution Class List

icon picker
Code Clips

Display to screen (Python console)

basic display

print('Add text here') # with a new line
print('Add text here', end=' ') # stay on the same line, set end to space character
print('\n\n') # display blank line (2)
Key Points about using print()
place message in round brackets ( )
place text in straight 'single quote marks ' (not curly ones) (double quote also OK)

display message with variables and text

print(number + ' + ' + str(number) + ' = ' + answer)
Display variables together in a message by concatenating text and variables.
Notice the spaces inside ' text '
The + concatentation operator is exactly the same symbol as the addition operator
When using concatenation, python requires you to cast non-string values/variables to a string value by using the str() function
If you want to be sure that numbers are added arithmetically then put them in brackets: print(player + str(i+1)) and not print(player + str(i) + str(1))
python offers another way to display multiple items: use commas
automatic conversion to string values, automatic space inserted
please note that this is not concatenation
print ('The score is', score, 'and the type is', type(score))

Variables

you do not need to declare variables in python
when you assign a (new) values to a variable, it takes on the type of that value
(python is a dynamically typed-language)
variable names should be descriptive and always used with exactly the same spelling and case
str - a string stores text, any combination of (letters, digits and symbols)
textVar = '' # assign a (empty) string value
int - stores positive or negative Integers (whole numbers)
intVar = 0 # assign an integer value
float - stores decimal numbers
floatVar = 0.0 # assign a real number/ decimal number/ floating point
There is no chracter type in python, simply use a string of length 1
char - stores single characters which can be letters, digits or symbols.
characterVar = 'F' # assign a single character string
bool - the values True or False
booleanVar = True # use a capital T or F
checking the type of a variable
you can check the type of a variable by using the type() function
score = 50
print('The score is ' + str(score) + ' and the type is ' + str(type(score)))
>> The score is 50 and the type is <class 'int'>

Keyboard Input, Storing With Variables

All input is read as text/string.
You should cast input values to the required data-type before storing into a variable
python will not warn you if you enter the wrong type of value until later if you try to use it for an operation
String - stores text; any combination of (letters, digits and symbols)
textVar = input('Insert prompt here: ')
Integer - stores positive or negative whole numbers
intVar = int(input('Insert prompt here: '))
float - stores decimal numbers
floatVar = float(input('Insert prompt here: '))
Character - stores single characters which can be letters, digits or symbols
this is exactly the same as for string/text
charVar = input('Insert prompt here: ')
Boolean - stores true or false
you must enter the values 1 for True and 0 for False for this to work
booleanVar = bool(int(input('Insert prompt here: ')))

Text operation

Concatenation of text
join text in username variable to the literal text, store as eMailAddress.
The + concatentation operator is exactly the same symbol as the addition operator
When using concatenation, python requires you to cast non-string values/variables to a string value by using the str() function
If you want to be sure that numbers are added arithmetically then put them in brackets: print(player + str(i+1)) and not print(player + str(i) + str(1))
emailAddress = username + '@gmail.com'

Arthimetic Operators

Addition
This code adds two numbers together and assigns the result to the additionAnswer variable
additionAnswer = numberOne + numberTwo
keeping a count by adding on 1
If you had to count how many keys are on your keyboard you might check along the rows thinking: 1, 2, 3, … It’s a very common task where you add on 1 to your count. In fact you a re following a well known algorithm:
start count at 0
add 1 to count to update it
add 1 to count to update it again ..
..
add 1 to count (keep going up to the last key on the keyboard)
display count
in Python you can add on 1 to a count variable like this
count = 0
count = count + 1
count = count + 1
..
count = count + 1
print('The final count is ' + str(count))
Subtraction
This code subtracts numberTwo from numberOne and assigns the result to the subtractionAnswer variable.
subtractionAnswer = numberOne - numberTwo
Divsion real number division
This code divides numberOne by numberTwo and assigns the result to the divisionAnswer variable.
The type of answer is float
divisionAnswer = numberOne / numberTwo
Divsion integer division
This code divides numberOne by numberTwo and assigns the result to the divisionAnswer variable.
The type of answer depends on numberOne and numerTwo
If they are both integers int then the answer will be an integer so 9/2 will be 4 (remainder 1 is thrown away)
If one or both numbers are decimal (double) then the answer will be a double (9.0/2 is 2.5, 9/2.0 is 4.5, 9.0/2.0 is 4.5)
divisionAnswer = numberOne // numberTwo
Multiplication
This code multiplies two numbers and assigns the result to the multiplicationAnswer variable.
multiplicationAnswer = numberOne * numberTwo

Conditional constructs and comparison operators

take care to position the start and end of IF…THEN and IF…ELSE constructs in the correct place
make sure that the code you want to be nested inside does indeed appear between the start and the end.
the nest is created by indenting 2 spaces from the left-hand margin
it is a good idea to reformat the program code after adding in a new construct, use replit auto format feature.

IF…THEN Construct

see below for examples of expression with comparison-operator
code nested inside the construct is carried out when the conditional expression is True
otherwise all the nested code is ignored
if expression_with_comparison-operator :
# one or more conditional statements
# end if

IF…ELSE construct

see below for examples of expression with comparison-operator
when the conditional expression is True then code nested inside the first part of the construct is carried out, code in the second part is ignored
otherwise the code nested in the bottom part is carried out and code in the first part is ignored
if expression_with_comparison-operator :
# one or more conditional statements
else :
# one or more conditional statements
# end if

Comparison Operators

the comparison operators are used in the head of the IF..THEN and IF..ELSE constructs
basic format is LHS comparison-operator RHS
One of or both LHS and RHS can be variables
The value in the variable(s) will cause the conditional expression to be either true or to be false
Equal to comparison operator
if variableName == 7 :
Not Equal to comparison operator
if variableName != 7 :
Greater Than comparison operator
if variableName > 7 :
Less Than comparison operator
if variableName < 7 :
Less Than or Equal to comparison operator
no more than 7, 7 or below
if variableName <= 7 :
Greater Than or Equal to comparison operator
at least 7, 7 or more
if variableName >= 7 :

Fixed Loop construct

take care to position the start and end of a loop constructs in the correct place
make sure that the code you want to be nested inside does indeed appear between the start and the end.
the nest is created by indenting 2 spaces from the left-hand margin
it is a good idea to reformat the program code after adding in a new construct, use replit auto format feature.
for i in range(0, stopNumber, 1) :
# one or more statements to be repeated
# nested inside the loop
# end loop
often you may want the user to enter a number to set the number of loop cycles needed
# prompt for how many times the loop has to repeat
cycles = int(input('How many loop cycles are needed? '))
# use the variable as the stopNumber for the loop
for i in range(0, cycles, 1) :
# one or more statements to be repeated
# nested inside the loop
# end loop
using the value of the loop variable to count up on each cycle
to display a count from 0, display i
to display a count from 1, display (i+1)
for i in range(0, cycles, 1) :
# display the loop variable from 0, display a count from 1
print('The value of i is: ' + str(i))
print('This is cycle number: ' + str((i + 1)))
# end loop

Arrays

An array in python can can be implemented as a list to store multiple values in a single variable. List elements in python can store values of different types.

Declare array, display array element

Array of Strings
# sets up an array of strings with list of values
textArray = ['text A', 'text B', 'text C']
# prints out first element in array
print(textArray[0])
# prints out second element in array
print(textArray[1])
Array of Integers
# sets up an array of integers with list of values
intArray = [1, 23, 456, 789]
# prints out first element in array
print(intArray[0])
# prints out second element in array
print(intArray[1])
Array of floats
# sets up an array of floats with list of values
floatArray = [0.1, 2.3, 4.56, 78.9]
# prints out first element in array
print(intArray[0])
# prints out second element in array
print(intArray[1])
Array without values
To set up an array of any type before assigning values to array elements
# sets up an array of 5 elements for any type
arrayName = [None] * 5
# find out how many items in the list to be stored
numberOfItems = int(input('Enter number of values you want to store: '))
# set up array and set the size to match the number of items
arrayName = [None] * numberOfItems

working with an array

use a variable as an array index
# sets up an array of strings with list of values
textArray = ['text A', 'text B', 'text C']

Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.