Learn Data Types Variables and Input
Learn Data Types Variables and Input
being
recorded
APPLY TASK ANSWER
WEEK 2
Starter
Understand how to use, data types (integer, real, Boolean, char, string)
Understand the need for, and how to use, variables and constants
Textbook Reference
For example
controlling the number of times a loop is executed
determining which branch of an IF statement is taken
keeping running totals and holding user input.
When algorithms are converted into programs, the computer needs to be told what type of data is stored
in each variable. This is called CASTING or ASSIGNING or INITIALISING .
For example, the result of multiplying a value by 5 differs according to its data type.
When we want to write computer programs we often need to store simple pieces of data temporarily in the
program for further use.
We do this using a combination of constants and variables, using the format name = value.
Whenever we store data that doesn’t change we store it in a constant, usually designated in Python as all
CAPITALS.
For example if we want to store the value of Pi, we can create a PI constant.
PI = 3.142
Variables
If we want to store the value of some data that might change, we use a similar system, but instead we use
lowercase letters.
player_name = "Bob“
A variable is a named space in a computer’s memory where we can store things and that we can change
(vary) the things that are stored in that memory.
It is important to note that Python and many other programming languages don’t recognise the difference
between variables and constants and so they will let you change a constant, even if you shouldn’t!
Python Naming Conventions
In Python, following naming conventions is important for writing clean and readable code. Here are some
commonly accepted naming conventions in Python:
Variable Names
Use lowercase letters and separate words with underscores to improve readability. For example:
my_variable = 10
user_name = "John”
Function Names
Use lowercase letters and separate words with underscores for function names as well. For example:
def
calculate_average(numbers_list):
pass
Python Naming Conventions
Constant Names
Use uppercase letters and separate words with underscores for constants, which are values that do not
change.
For example:
MAX_VALUE = 100
PI = 3.14
Remember that these conventions are not strict rules enforced by the Python language itself, but they are
widely followed in the Python community.
Consistently applying these naming conventions will make your code more readable and make it easier for
others to understand and collaborate on your code.
Python Naming Conventions
Variables can contain all kinds of information. Let’s look at a few examples:
https://www.youtube.com/watch?v=9QIFXyBYJQY&list=PL04uZ7242_M5F0O5YicE_oxG0e2rvWq-q&index=5
Plenary
Use the following summary and video link to make notes for your Apply task.
https://www.youtube.com/watch?v=9QIFXyBYJQY&list=PL04uZ7242_M5F0O5YicE_oxG0e2rvWq-q&index=5
Syllabus
APPLY TASK
Check your ‘to do’ list
for your Apply task
Deadline 1 week
Lesson complete!
See you next lesson