In this lesson, we'll get started with the basics of programming in Python.
Let's start with making a new file in PyCharm.
The process for this is pretty straightforward and it gets more comfortable every time you do it.
You'll need to keep all of your files within this venv
folder.
In order to make a new file, I'm going to right click on the venv
folder.
Go to New and I'm going to navigate to where it says Python File.
This will ensure that I can get the Python .py
at the end of the file, even if I forget to type it myself.
Since we're starting with variables in this chapter, I'm going to go ahead and type “variables”.
And then I could type “.py”, that would be fine.
But because I've selected the Python file as the file type in advance, I have a file that has the proper naming.
You'll notice that I started with a number sign or hashtag (#
) at the beginning of line 1. And you'll also notice that the text is grayed out.
In Python, this is how you create a single line comment.
This single line comment is a way that you can take or add notes in your Python program and these notes can indicate to you what's happening. So, it can be helpful while you're working.
Even professional programmers use these notes to help indicate what they've done in the code.
Like other programming languages, Python has variable naming conventions that are used to help Python read your program and also to make your variables clear to you and to other people who might be looking at your code.
Variables can either be named with lower-case or upper-case letters or numbers. And they can start with an underscore also.
For example, we can have this variable that starts with the underscore and we can have the same name without an underscore.
_cars = 23
cars = 24
CARS = 25
Note that these are all different variables — your variables are case sensitive.
The variable CARS
with capital letters is different than the variable cars
with lowercase letters.
Your variables should be descriptive.
When you use one-word names for your variables, that's easy. If you have more complex variables, you might want to use multi-name variables.
If you’re using a multi-name variable, you need to make sure that you’re using underscores between each of the words.
number_of_cars = 23
kind_of_cars = "Ferrari"
Now that we have these variables, it would be good to see the outcome of the variables.
We'll go ahead and navigate to the Run option in PyCharm.
When you click Run, you can run multiple files.
As you'll see, something else that I was working on previously comes up.
So, what you want to do is go to the Run option with the ellipsis dots.
When you select this option, then you'll actually be able to take a look at all of the different files that you have available.
I'm going to navigate to the “variables” file. Hit Enter.
And you'll notice that the file ran and finished with an exit code zero which is good.
That means there are no errors.
I have variables, but when I run the code, nothing happens.
In order to see the output, I'm going to use the Python print
statement.
I'm going to use print
and then parentheses, and then within, I'm going to put the name of the variable that I would like to print.
You'll notice I have several variations of the word “cars”.
I'm using print
to validate that each version of the word “cars” behaves as a unique variable due to case sensitivity.
Since the variables are different, one should not overwrite the other.
You'll also notice that PyCharm provides me a dropdown for my variables so that way that I can easily use my keys, select and have automatic line completion.
print(cars)
print(_cars)
print(CARS)
print(kind_of_cars)
Now that I have my print
statements written, I'll go ahead and select Run.
And as you can see, I have my lowercase cars, underscore cars, and capital cars as well as my kind of cars output.
What happens if you use the same variable twice in a row?
Notice how Python uses the last value of cars when there is more than one variable with the exact same name.
Let's talk about Strings.
In Python, Strings are enclosed in single or double quotes.
It doesn't matter which you use as long as you're consistent with your style or the style of your workplace.
Strings are immutable.
This means that operations cannot be performed on Strings. Some examples of strings are above including "Jess Ingrassellino", "345" and "violin".
An integer represents a whole number and mathematical operations can be performed on integers in Python.
When you type integers, there are no quotations around the numbers.
Some examples are above. You'll see that 22, 34 and 589182 are all individual numbers without any quotes or other syntax surrounding them.
Floating-point numbers are fractional or decimal numbers on which mathematical operations can be performed.
Python is different from some other programming languages in that you don't need to specify the number of decimal places before you create the floating-point number.
Python will recognize what it is that you want and will accommodate accordingly.
You can see some examples of floating-point numbers: 3.14, 9.86 and 234.09823.
In this file example, I'm going to get started by using the multi-line comment code. This is really useful for docstrings when you're writing long lines of code and or taking notes of a longer nature.
So instead of having to put a hashtag in front of each line, if you can use the comment.
It's pretty simple; 3 quotes to start and 3 quotes to end.
Now we're going to take a look at string formatting.
"""
This is a multi-line comment
You can use this kind of comment to
make longer notes as you are learning.
In python, these are often used as
docstrings.
"""
name = "Janelle Jones"
type_of_car = "Rolls Royce"
school = "Foundation Elementary School"
String formatting is great because you can actually be pretty specific in Python about how you need output to appear.
But if you are printing messages to users like error messages or other interactive messages, then you do want to make sure that you're formatting your output in a way that makes sense, that it's pleasing to read and that allows you to be really clear and use your variables without extraneous work.
Python offers the String formatting method which allows you to specify.
So first we're going to do a standard string format so you can go ahead and type along with me.
TIP
I really hope in these videos that you are coding along — pressing pause and taking time to make sure that you are getting the same results in your IDE.
We're going to do a standard just printing the variables like we have and using the plus sign that will put the two variables together, concatenate them.
print(name + school)
So, it doesn't add them in a mathematical sense. It puts prints them one after the other.
You can see our first outcome is pretty ugly.
“Janelle Jones” and there's no space between the name and the “Foundation Elementary School”.
We can go ahead and add a space by including that syntax for the quotes and then an actual space in our print line.
print(name + " " + school)
It's still not great.
We don't have a ton of control and we have to write a lot of extra code.
We can try to do some things to clarify our output a little bit more, and sometimes we want to communicate a message that will use our variables but also has other components to it.
print(name + " works at " + school + ".")
It's pretty simplistic and there's quite a bit of code.
We can actually do this with more efficiency, and we'll do that by going and using the Python formatting method.
#python string.format()
print("{} works at {}.".format(name, school))
So, you'll notice that in using the formatting method, I'm using curly braces in each of the places that I would want to put variables.
How are we getting the variables there?
Luckily for us, the format
method or format
function actually accepts the arguments of our variables and transfers those arguments into the curly braces in the order that they appear in the sentence.
So, it makes it really easy and really clear to understand the variables and how they actually arrive.
When we print that out, we can see that our code is clearer.
The intent is more obvious.
So, all in all, the format
method is a great way to make sure that your code is clear and saying exactly what you want it to say.