Welcome to the second lecture in our Python Crash Course for Beginners series.
This lecture will focus on the syntax of Python and understanding how Python code is executed.
We have installed Python 3.10.4 already in our systems. Now we need a code editor for Python. There are a plethora of options out there but my favorite is PyCharm.
It is one of the most popular code editors used for Python and we will be using it for this course. It looks like this on the first start.
Now create a new Python file in the project. now you are ready to write your first code in Python.
Now expand the “Run” menu and click run. you can also use the hotkeys ‘Shift + F10’ in windows or ‘Ctrl + cmd + R’ in the macOS to run the program.
After that, you will see a small terminal window at the bottom showing the output of your code. This was your first Python code. Now we will learn how it was executed.
This was your first Python code. Let’s try another example.
In this example, we will print 10 asterisks ‘*’ by simply multiplying one asterisk by 10.
The output will look like this.
Now we will learn how it was executed.
How Python Code is Executed?
When we write a code in Python, the piece of code inside the parenthesis i.e. (“*”*10) is called an expression. It is the piece of code that produces a value.
So when the Python interpreter tries to execute line 2, first it will evaluate the code in between parenthesis i.e. the expression. Hence it will produce 10 asterisks and print them on the terminal.
Variables in Python
Variables are one of the most fundamental concepts of programming and they are not specific to Python. We use variables to temporarily store data in the computer’s memory.
Here’s an example,
Price = 10
When the Python interpreter executes this code, it will allocate some memory in which it will store the number 10. Finally, it will attach this price to that memory location.
Now we can use this variable “Price” anywhere in our program to access the value that we have saved in that variable.
Now if you type,
The output of this program will show the stored value in the variable “Price” i.e. 10. Note that the variables are case-sensitive.
So this is how we define variables, we start with an identifier which is the name of our variable, then an equal sign and finally a value.
Let’s take this program to the next level. On the second line, we can update the value of this price variable to a new value. Let’s make it 20. Now when we run our program, we should see 20, because the Python interpreter executes our code line by line from the top.
We can store integers, floats, strings and boolean values as variables. Besides, Python can store complex values like lists and objects which we will discuss later.
Now we will observe the example of string and boolean values.
In Python, a string is a sequence of Unicode characters. In simple words, it is any series of characters that are interpreted literally by a script.
name = 'HashDork'
Similarly, boolean values are used to assign a True or False value to an expression.
is_published = True
Note that Python is a case-sensitive language and normally you should use lower case letters to write the variable and start with the upper case while defining a boolean value. In this case, ‘true’ won’t be recognized by the interpreter and will result in an error. Underscore is used to separate words in a variable name.
Now, we will see an example program that will utilize the above concepts.
Imagine we’re going to write a program for a hospital. So we check in a patient named Raj Koothrappali (Yeah, you got it). He’s 29 years old and is a new patient. I want you to define 3 variables here, his name, his age, and another variable to check if this is a new or an existing patient.
Stop scrolling and try it by yourself.
Now let’s get the solution.
full_name = 'Raj Koothrappali'
age = 29
is_new = True
That’s all from this lecture.
We will learn about receiving input in Python, Type Conversion and Strings.