Tech tutorials, howtos and walkthroughs

Displaying or “printing” information to the screen in Python is done using the print() function. So although we have been fiddling with datatypes up to now we haven’t displayed them, so first let’s try doing that using the variables we were experimenting with before:

x = 3
y = 2.2
z = “Cuba”
i = True

*save the file as geezam_python.py – ensure its a dot py file which is the python file type

To show each of them on screen we type the print function with the name of the variable within brackets like so:

print() function

print(x)
print(y)
print(z)
print(i)

Click run in Thonny and voila! the values displayed on the screen. We can also put values directly in the print function by entering them within the brackets. just remember that when it comes to strings we need to use the double or single quotes:

print(100)
print(‘rawr’)
print(“loud rawr”)

type() function

In the previous post, I talked a little about the type() function. Lets practice combining it with the print function to show the data type of a variable on screen. We do that by putting the type function within the print function like so; print(type(x)) and voila! you can see the data type of any variable. let’s do it for all our test variables so far

print(type(x))
print(type(y))
print(type(z))
print(type(i))

you can now see what we are learning on the screen: x is an integer, y is a float, z is a string and i is a Boolean.

more on Geezam.com:  Using the Steam API with Python to access your data

commenting

Putting comments in your code is a way to make it more readable, allows the programmer to explain parts of code to others who might read the code, like a teacher or team member and also can be used as a way to ignore batches of code you don’t want to run. Looking at the code we have so far in our geezam_python.py lets put some comments in. To do so we use the hash special character #

# declaring variables
x = 3
y = 2.2
z = “Cuba”
i = True

# printing with and without variable names
print(x)
print(y)
print(z)
print(i)

print(100)
print(‘rawr’)
print(“loud rawr”)

# printing variable type
print(type(x))
print(type(y))
print(type(z))
print(type(i))

save and click run and you’ll notice the comments are ignored by the program, but a human reading your code (including yourself) can make sense (hopefully) of the code below. But what if you want to make comments on multiple lines? we do that with triple quotes, just remember that just like strings stick with the type of quote you use and don’t mix single and doubles quotes. Let’s go back to the top of the saved file and test it out.

“””Geezam.com
Let’s
learn
Python
Together “””

Recap

more on Geezam.com:  Manage Passwords for free with Bitwarden

we use the print() function to display content on the screen. Use the type() function to find out the type of a variable and use the hash character for single-line comments are triple quotes for multiline comments. Take your time experimenting with what we have covered so far and when you are ready let’s jump into the next post in the series about operators.

Python Programming Tutorial for Beginners

About the Author

Read more on Geezam.com