Welcome to Day 3 of #100DaysOfPython. Today we'll look at some Python code and learn about strings, variables, and functions.
Below are some Python snippets of code. Try to guess what they might do!
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
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
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
y contain the values
That means that from then onwards, we can use
x instead of
y instead of
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
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?
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.
We’ve not told Python about
We have not told Python about
At some point before your code runs, there is a line of code similar to:
print = ...
And what about the brackets?
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
The difference between accessing the value of the function and executing the function is in whether we use the brackets or not. For example:
Would print out the description of the function
We’ve learned about…
- How programming languages almost always assume they are running in a text interface, so functions like
- 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
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!