Python Basics – Print, Data types and comments

Basic Data types in python. These are the data types we will look in great detail for this tutorial.

Data Types Meaning
String Basic text in python. Usually seen between quotations used to show text on the screen
Integers / Floats / Numbers Integers are whole numbers such as 1, 10, 22, 55, 102. There is no decimal numbers allowed in integers.

Floats are numbers with decimal points for example 2.1, 2.2 40.1 or £59.99.

Lists / Array Multiple values stored in a single variable. This is called Arrays in other programming language and works the same in python.
Boolean It only carries a true or false value. Can be used in IF statements to utilise a certain function.

Part two: Learning the data types: String in python programming

we have looked at print and comments in the last part. Now we will look into string and what it does. String is a type of variable. Variables are used in programming languages to assign a value or to save a certain information in them until the program stops running.

You have been using them all the time without know what they are. In your games you have used them as score, high score or other information, all these information which is being stored are variables. Important thing to know about variables are it runs in the background inside the RAM (Random Access Memory).

To declare a variable in python is very easy simply write:

name = “John Smith”

That’s it. In many other programming languages we will need to tell it what kind of variable it is before assigning a value but in python it will automatically pick it up once it checks the initial value itself. Less work for you to do. Nice right.

Lets work together with string and print.

name = “John Smith”
print(name)

This will print John Smith on the console. What just happened? To explain simple look at the top line it says name inside of name it has John Smith value. the print function has name (no quotations) thus it looks inside the name variable to find John Smith value.

Try this one

name = “John Smith”
print(“name”)

This will print the value name. Why?? When we put in the code name inside the quotation that became a string itself and python printed that string to the display instead of the variable value. So its important to note that if you want python to look inside a variable then don’t put it inside any quotations. Got it. don’t make me repeat it, if you forget just read it again lol.

Changing the string in python

A string variable or any variable in python can be changed. Not everyone will be called John Smith right. Well lets see this in practice

name = “John Smith” # declare a variable
print(name) # print the name variable
name = “Mike” # change the variable value
print(name) # now it prints mike

What just happened? Let’s look at closely. Look at the code first we said the name was john smith and it saved john smith value inside the name string. Then we asked python to display the name to screen which still is john smith. Now we changed the name to mike and python saved the value mike inside the name variable. When we asked it to print the value of name variable to the screen it printed mike. The last change is the recent one and that’s the one python remembers. If you type print(name) again under the last line it will print mike again on the screen, john smith has been erased. Poor guy tut tut.

Now lets add some strings together to see what happens.

in your programming journey you will notice a few things come up. You will never have just a single variable and you will sometimes need to join them together and do some work.

Do the following:

name = “Mike” #name of the person stored
city = “London” #city of this person lives in
print(name, city) # print the name and city

This will print out Mike London. sounds like a cool name but not what we intended for this exercise. Try again like this:

name = “Mike” #name of the person stored
city = “London” #city of this person lives in
print(“Your name is: “, name, “You live in: “, city) # print the name and city

This will return Your name is: Mike You live in: London. makes more sense this way right. We have added some additional strings with our string variables to make more sense to HUMANS.
Now add your own and make sure to use commas appropriately in the print function.




Comments are closed.

%d bloggers like this: