1. Introduction: Syntax
Python Syntax
In this chapter, you will make yourself familiar with the syntax of the Python programming language. Before exploring the material any further it is important to know the formal definition of syntax in the context of programming languages.
The syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be correctly structured statements or expressions in that language.
Python's syntax promotes readability, it is therefore simpler and more intuitive compared to other programming languages, which makes it a very suitable language for learning the fundamentals of programming. In the coming chapters, we will discuss the Python syntax in detail. It is important to respect the syntax of a programming language because failing to do so will result in your program simply not working.
Before we continue, it is important to take a look at several key definitions used to describe coding syntax. These definitions will be used throughout the entirety of the course. For each definition, we provide a loose example of the syntax it references.
Definition | #\qqquad#Example | Explanation |
A variable is a storage location paired with a symbolic name, which contains some quantity of information referred to as a value. | #\mathtt{\text{month} \color{#3e999f}{\text{ = }} \color{#F5871F}{\text{2}}}# | The value #\color{#F5871F}{\mathtt{\text{2}}}# has been allocated to the variable month. The value for month could possibly change when the program runs. |
A constant is a value that is not altered by the program during execution. It can be a pre-defined value or a value defined by the programmer. | #\mathtt{\text{PI}\color{#3e999f}{\text{ = }}\color{#F5871F}{\text{3.14}}}# | The value #\color{#F5871F}{\mathtt{\text{3.14}}}# has been allocated to #\mathtt{\text{PI}}#. This value is not meant to change when the program runs. |
An object is an instance of a certain type or class that contains data. | #\color{#F5871F} {\mathtt{\text{10}}}# #\color{#718c00} {\mathtt{\text{"string"}}}# #\mathtt{\text{[}\color{#F5871F}{\text{1}}\text{, }\color{#F5871F}{\text{2}}\text{, }\color{#F5871F}{\text{3}}\text{]}}# |
For example, numbers are actually objects, so are strings. A list is also an object, but an object containing objects. |
A function is a sequence of instructions packaged as a single callable unit with a symbolic name. A built-in function is a function that is provided by the language itself. | #\begin{split} & \mathtt{\color{#4271ae}{\text{len}}\text{(}\color{#718c00}{\text{"SOWISO"}}\text{)}}\\ & \rightarrow \color{#F5871F}{\mathtt{\text{6}}} \end{split}# |
When the function #\mathtt{\color{#4271ae}{\text{len}}\text{()}}# is called with the string #\color{#718c00}{\mathtt{\text{"SOWISO"}}}# as argument it returns the number of characters in the string. |
A method is a function associated with an object. As opposed to a function, a method is called on an object. | #\begin{split} & \mathtt{\color{#718c00}{\text{"sowiso"}}\text{.}\color{#4271ae}{\text{upper}}\text{()}}\\ & \rightarrow \color{#718c00}{\mathtt{\text{'SOWISO'}}} \end{split}# |
When the method #\mathtt{\color{#4271ae}{\text{upper}}\text{()}}# is called on the string #\color{#718c00}{\mathtt{\text{"sowiso"}}}# it returns a new instance in uppercase. |
An operator is a compact construct defined by the programming language that generally behaves like a function, but differs semantically or syntactically. | #\begin{split} & \mathtt{\color{#4271ae}{\text{int}}\text{(}\color{#F5871F}{\text{1}}\text{).} \color{#4271ae}{\text{__add__}}\text{(}\color{#F5871F}{\text{1}}\text{)}}\\ & \rightarrow \color{#F5871F}{\mathtt{\text{2}}}\\ & \mathtt{\color{#F5871F}{\text{1}}\color{#3e999f}{\text{ + }}\color{#F5871F}{\text{1}}}\\ & \rightarrow \color{#F5871F}{\mathtt{\text{2}}} \end{split}# |
Instead of the quite complex syntax that actually computes the sum of #\color{#F5871F} {\mathtt{\text{1}}}# and #\color{#F5871F} {\mathtt{\text{1}}}#, Python provides the operator #\color{#3e999f}{\mathtt{\text{+}}}#, which behaves the same. |
A keyword is a word reserved for a specific function within the syntax, it can therefore not be used as an identifier for variables or functions. | #\mathtt{\color{#F5871F}{\text{True}}\color{#8959A8}{\text{ or }} \color{#F5871F}{\text{False}}}# | This is an expression with three keywords. Keywords are recognized by the text editor and distinctly highlighted in either purple or orange. |
An expression is a syntactic entity that can be evaluated to determine its value. It can be a combination of all of the above. | #\begin{split} & \mathtt{\color{#4271ae}{\text{len}}\text{(}\color{#718c00}{\text{"SOWISO"}}\text{)}\color{#3e999f}{\text{ - }}\color{#F5871F}{\text{3}}}\\ & \rightarrow \color{#F5871F}{\mathtt{\text{3}}} \end{split}# |
An expression can be as complex as you'd like. In this case, we subtract the result of a function, #\color{#F5871F}{\mathtt{\text{6}}}#, with #\color{#F5871F}{\mathtt{\text{3}}}#, resulting in the value #\color{#F5871F}{\mathtt{\text{3}}}#. |
Or visit omptest.org if jou are taking an OMPT exam.