Variables & Data Types
Subscribe to Tech with Tim
Before starting please ensure that you have downloaded and installed python 3.0+ from the python website.
A Little Bit About Python
Python is a very beginner friendly language. Its syntax (words, structure and commands) is very similar to English which allows it to be learned quickly. It is an extremely powerful language that is used in a variety of different applications. Python is most commonly used for web design but is also capable of making games, performing data analysis, controlling robots, creating GUI's and much more.
The main advantage python has over other languages is its simplicity. This allows for programs to be written and tested much faster than many other languages.
Whenever we are writing python code we want to use some sort of a text-editor or IDE (Integrated Development Environment). Luckily for us when we install python it comes with its own editor named "IDLE". IDLE has two main components, a text-editor and a shell. The shell is what will appear after launching the program. In the shell you can type any python code you wish and after hitting enter it will immediately be executed. This is great for testing small snippets of code but it's not what we want for right now. To get started we need to get to the text-editor built into IDLE.
To do this simply find "IDLE" and double-click to run it (you can type IDLE in your windows search bar and it should show up).
Once we've done that we need to click File > New File from the top menu bar. This should open up what looks like a blank file.
Once we've done this I recommend saving your file by hitting CTRL + S or by clicking File > Save As.
And now we are ready to start coding.
A "Data Type" is simply a classification of information. The data type of an object dictates the different things that we can do to it and use it for.
In Python there are many different data types but the ones we are going to talk about now are the following:
Integer: In python integers are represented by the keyword "int". Int's are any whole number (negative or positive).
Float: In python floats are represented by the keyword "float". Floats's are any decimal number (negative or positive).
String: In python strings are represented by the keyword "str". Str's are any anything surrounded by double quotes ("im a string") or single quotes ('i am also a string').
Boolean: In python boolean's are represented by the keyword "bool". Bools are either True or False (Capitals Matter!).
int: 1, 3, 5, 9, -3, 0 float: 1.0, 0.9, -0.7, 5.6, 8.0 str: "hello", "0.2", '-98', 'True' bool: True, False
A variable is something that we use to hold a value. We can create are own variables that hold and store values by typing a variable name and then setting it equal to a certain data type (see below). A valid variable name is anything that does not start with a number and contains only letters and underscores (NO SPACES). Capitals in variable names are allowed but do make a difference: the variable tim is NOT the same variable as Tim.
tim = "hello" myVariable = True
To reference our variables we simply type their name. We can easily change variables that already exist by typing that variable name and then setting it equal to a new value.
tim = "hello" myVariable = True myVariable = 3 # myVariable now is equal to 3 joe = myVariable # joe is now equal to 3 because myVariable has the value 3
Displaying Things to The Screen
To display something to the shell in python we can use the keyword "print":
var = "hello" print("hi") print(var) # This will print "hello"
Note: To run your program and see the output click the F5 key or from the menu bar: Run > Run Module.