Get startedGet started for free

Python Syntax

1. Python Syntax

We've discussed why we should use Python; now, let's learn some Python syntax! To begin, let's talk about writing code that produces results. To demonstrate this, we'll use Python to perform simple calculations. We can instruct Python to perform computations using symbols called operators. We can use the + operator in the Python shell to add four and five. Once we hit enter, the Shell will display nine, which is the output of our calculation. Now that we have seen how to perform basic calculations from the Python Shell let's do the same using the Python script. We will use the built-in Python print function to display the result of our calculation. To do this, we first write the word print followed by an opening and closing bracket. Next, we put our calculation four plus five within the brackets and then hit the "Run code" button. We will then see the result of our calculation inside the Shell. Python’s print function is the only way to show the output of code written within our Python script. We used the + operator to perform simple addition, but Python has more calculation operators. The other operators are the minus operator, a hyphen used for subtraction; the asterisk (*) operator for multiplication; and the division operator, denoted by a forward slash (/). Python includes other operators, like modulus (%), which calculates the remainder. However, these are beyond the scope of this course. Finally, let’s discuss code comments. Since code is read by developers, comments help others understand what our code is doing. A line starting with `#` is a code comment. Code comments don't affect the code's execution but are typically used to document and explain what a piece of code is trying to do. It is your turn to use Python to try out these operators and perform your calculations.

2. Let's practice!