Part 2 - Learn Text Based Coding In Python
If you would like more detailed information about python programming we recommend w3 school's python tutorial to learn more & w3 school's python exercises to test your knowledge.
Introduction
Hello, welcome to this guide where we will be learning Python.
Before we start you will need python please follow our instructions to get python
Part 1 - Setting up pythonMake a Hello World program
Type print("Hello, World!")
What does it do?
Variables
This video talks about variables and how they work
What are variables?
You can think of variables as labeled containers that store a value.
When defining a variable in python you first have to give the variable a name then use the equal sign which is used to assign values to the variable.
So in a nutshell to create a variable you need to give your variable a Name, Value, and Data Type(In python the Data Type of your variable is found automatically)
Example:
greeting = "Hello"
Data Types
In python there are 4 main Data Types:
String
The String Data Type which is used to store text, to tell the computer that something is a string you just have to surround it with quotes, both ' '
and " "
work, anything you surround by quotes is a string so numbers surrounded by quotes are text, not a number.
Integer
An Integer is a whole number meaning no decimal point
Float
A Float is a decimal number meaning it always has a decimal point even if it ends with .0
Boolean
A Boolean is just True or False, in python you have to write True and False starting with a capital letter otherwise it doesn't know what it is.
Example:
myString1 = "Apple"
myString2 = 'Banana'
myInt = 10
myFloat = 1.5
myBool = False
Operators
Mathematical Operators
Operator
Name
What it does
+
Add
This operator is used to add to numbers together, it can also be used to join (Concatenate) Strings
-
Subtract/Concatenate
This operator is used to take one number from another
*
Multiply
This operator is used to multiply one number by another
/
Divide
This operator is used to divide one number by another
%
Remainder of
This operator is used to divide one number by another and output the remainder
**
Power of
This operator gets the power of one number by the other
Assignment Operator
The assignment Operator in python is a single equal sign, it is used to tell the computer that one thing is equal to another.
Comparison Operators
Operator
Name
What it does
==
Is equal to?
>
Is greater than?
<
Is less than?
>=
Is greater or equal to?
<=
Is less than or equal to?
!=
Is not equal to?
If/Else Statements
To write an if statement you first need to use the keyword if
followed by the condition you want to check then you end the line with a :
then on the next line which should be indented you type the code you want to run if the condition is True, you can then use the keyword elif
to check another condition if the first condition is not True and then you can use else
when none of the conditions are True.
Lists
In Python, you can store a list inside of a variable
To create a list you will first need a variable to store it in and to tell the computer that you are creating a list you have to surround it with []
then you can type some values in your list each one separated by a comma.
Example:
var = ["Apple", 15, False, 'Banana', 1.5]
Loops
While Loops (Conditional Loops)
A while
loop repeats as long as the condition is True
To create a while loop you first write the while
keyword followed by a condition and then a :
and on the next line, you will write the code you wish to repeat.
For Loops (Iterative Loops)
text
Example
fruit = ["Apple", "Banana", "Pear"]
for i in fruit:
print(i)
Output:
Apple
Banana
Pear
text
Last updated
Was this helpful?