/ 100DaysOfPython

Day 3: Key Python Building Blocks

Welcome to Day 3 of #100DaysOfPython. Today we'll look at some Python code and learn about strings, variables, and functions.


This post is part of #100DaysOfPython, check out yesterday's post here if you haven't already. Or go to the index of all 100 days.


Below are some Python snippets of code. Try to guess what they might do!

Snippet 1:

print("Hello, world!")

Snippet 2:

x = 15
y = 75
print(x * y)

Is it English?

As you can see, the words seem English, but they interspersed are symbols that we wouldn’t normally encounter in written language: brackets, curly braces, quotation marks, and even equal signs!

In Snippet 1, we tell Python to print something. What could that mean? Is it talking to your printer?

Most programming languages assume they are running on a text interface, not a graphical interface. For example, Terminal.app or cmd.exe can be used to run Python programs.

Thus, when we tell Python to print, it will output some text into the text interface used to run the application.

Storing things in Python

Python “knows” nothing, imagine it as a completely blank slate. We can tell it things, and for that we use a single equal sign. Whenever we use a single equal sign, the thing on the left acquires the value of the thing on the right. For example, on Snippet 2, we told Python that from now onwards, the letters x and y contain the values 15 and 75 respectively.

That means that from then onwards, we can use x instead of 15, and y instead of 75.

Python variables

Anything we type in Python that is separated by spaces and is not inside quotation marks is something that Python must be able to understand. That means, we must’ve told it what the thing equals at some point in the past.

Take Snippet 2 again. If we reverse the order:

print(x * y)
x = 15
y = 75

Then Python would give us an error, because when it evaluates print(x * y), it does not know what x and y are. They are declared later on.

However, we could do this:

print("x * y")
x = 15
y = 75

What do you think that snippet of code would print out?

Python strings

You would see the characters x * y, literally. It would not try to calculate what x means or what y means, because we’ve surrounded them in quotation marks. Now Python treats them as literals, and does not try to interpret their meaning. This is called a string.

What about print?

We’ve not told Python about print, and yet it is not within quotation marks. How does this happen?

We have not told Python about print, but someone else has. print is something that is loaded into every Python application you run, as it is part of the builtins of the language.

At some point before your code runs, there is a line of code similar to:

print = ...

And what about the brackets?

print is not a variable, like x and y are. It is a function.

Variables can be used to access the value inside them. Functions can be too, but they can also be executed. Functions contain some other code, and when we execute the function we run this code.

For example, the print function will contain some code to interface with the text interface and sends it the text you want to print.

The difference between accessing the value of the function and executing the function is in whether we use the brackets or not. For example:

print(print)

Would print out the description of the function print. Confusing, but just remember that functions are just variables that we can execute!

The first print has brackets, and thus would execute the function. Inside the brackets go arguments. In this case, the argument is what we want to print. The result is printing the print function!

Conclusion

We’ve learned about…

  • How programming languages almost always assume they are running in a text interface, so functions like print make sense;
  • What variables and functions are;
  • What quotation marks are used for—which is denoting strings; and
  • That functions can take arguments, and in case of the print function it takes what we want to print.

Don’t worry if not everything is crystal clear. By all means do more research, but also give yourself time. It takes months for programming to fully “click”, and everything to start making complete sense. Follow along and keep coding. You’ll be there in no time!

I'll see you on the next one!

Like what you see?

Join our e-mail list to receive new content as it's posted—once or twice a month!