0% found this document useful (0 votes)
33 views34 pages

Basic Syntax and Semantics

Uploaded by

momen6u5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
33 views34 pages

Basic Syntax and Semantics

Uploaded by

momen6u5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 34

Introduction to

Computational Thinking
Module 3 :
Basic syntax and semantics

Asst Prof Chi‐Wing FU, Philip


Office: N4‐02c‐104
Module 3 : Basic syntax and semantics email: cwfu[at]ntu.edu.sg
1 of 34
Topics
• Semantics and Syntax
• Let’s learn with a simple example
• More on Python Syntax

Module 3 : Basic syntax and semantics 2 of 34


Semantics and Syntax
When writing programs, you have to take care of
• Semantics – Meaning of your program
• Syntax – Specifying your algorithm using a
programming language

Problem Algorithm Program

Computational Programming
Thinking Run on
Syntax
Semantics

Module 3 : Basic syntax and semantics 3 of 34


Semantics and Syntax
Just like communication with English:
• The meaning (semantics) of the sentence,
as well as
• The grammar (syntax) of the sentence, so that
others can understand, e.g.,
√ he has X he have
√ we are X we is

Module 3 : Basic syntax and semantics 4 of 34


Semantics and Syntax
• Different languages have different syntax;
this applies to both spoken languages and
computer programming languages
• To make sure the Python shell can understand
your Python program, your program has to
follow the syntax of the Python language

This module talks


about basic
Python syntax

Module 3 : Basic syntax and semantics 5 of 34


Topics
• Semantics and Syntax
• Let’s begin with a simple example
• More on Python Syntax

Module 3 : Basic syntax and semantics 6 of 34


Code Listing 1.1
• To understand Python syntax, it is better to
take a working Python program as an example:

Module 3 : Basic syntax and semantics 7 of 34


Terminology #1) Statement

• Each line of code in a Python program is called a statement


• Python interprets and runs statements one by one

Module 3 : Basic syntax and semantics 8 of 34


Statement Continuation
Improve readability
in a text editor

• Python is sensitive to end of line in text files, which marks


the end of a statement; in text editors, we press “enter”
• The symbol \ is used to continue a statement with the next
line so that two lines can be joined as a single statement
(this is good for long statements… readability)
Module 3 : Basic syntax and semantics 9 of 34
Terminology #2) Comments
Comment lines

• The pound sign # in Python indicates a comment


• Anything after # is ignored for interpretation (in green)
• Comments provide information to improve code readability

Module 3 : Basic syntax and semantics 10 of 34


Terminology #3) Keywords
Light blue

• Python reserves certain words for specific purposes in the


programming language itself, e.g., import, etc. (light blue)
• You cannot use these words to define your own stuff; they
are called the reserved words
Module 3 : Basic syntax and semantics 11 of 34
Terminology #4) Modules
math module and dot operator

• A module is a Python file containing elements to help working


on a certain problem, e.g., math (see above) -> math.pi
• Modules are great resources provided by Python to perform
various common tasks, e.g., database, network, etc.
Module 3 : Basic syntax and semantics 12 of 34
Terminology #5) User Input
a function call to get input

• input is a built-in function provided by Python


• Prints the message string on the screen and waits till the
user types something (anything), ending with Enter
• Returns a string (a sequence of characters) no matter
what is given, even a number
Module 3 : Basic syntax and semantics 13 of 34
Terminology #6) Computation

main computation

variables
• Using the input radius, we can compute circumference
and area
• Note: = is not equal sign! It is an “assignment operator” in
most programming languages to assign values to variables
Module 3 : Basic syntax and semantics 14 of 34
Terminology #7) Print results

• print is another built-in function provided by Python;


it displays the related message and data on the Python
shell screen (note: use comma to separate elements)
• A single print() makes an empty line -> try several empty print()
Module 3 : Basic syntax and semantics 15 of 34
So altogether… this program?

• There are three steps in the program…


See the comments on top of the program!!!

Module 3 : Basic syntax and semantics 16 of 34


So you should know…
Basics…
• Statements and Statement Continuation
• Comments with #
• Keywords / Reserved words (e.g., import)
• Modules (e.g., math)
• Built-in functions (e.g., input and print)
• Variable and Assignment operator =
(more to come in next module)

Module 3 : Basic syntax and semantics 17 of 34


Topics
• Semantics and Syntax
• Let’s learn with a simple example
• More on Python Syntax

Module 3 : Basic syntax and semantics 18 of 34


Let’s look at more syntax stuff
• Comments
• Whitespace
• Indentation
• Tokens: Keywords, Operators, Punctuators
and Delimiters, Literals
• Expressions
• Interpreter Errors

Module 3 : Basic syntax and semantics 19 of 34


1) Comments
• Basics: Anything that follows # is ignored (by
interpreter/compiler) on that statement
• Though contributing nothing to the program
execution, comments are important things to
improve readability…
• But… No universal rules for right style and
number of comments

Module 3 : Basic syntax and semantics 20 of 34


1) Comments
• Useful guidelines:
• Why philosophy:
Good comments do not repeat the code or
explain it. They should clarify the intention
of the code and explain higher level concept
• How philosophy:
If your code contains a novel or noteworthy
solution, add some comments to explain it

Module 3 : Basic syntax and semantics 21 of 34


2) Whitespace
• Purpose: to separate words in a statement
• Python counts the following characters
as white space:
• Space, tab, return, etc. (see textbook)

• For the most part, you can place “white space”


(spaces) anywhere in your program; use it to
make a program more readable, e.g.,
a = a + 1 + c instead of a=a+1+c
math.asin( math.cos(a) + math.tan(b+c)*3 )

Module 3 : Basic syntax and semantics 22 of 34


3) Indentation
• Leading whitespace at the beginning of a
statement defines indentation, e.g.,
(will see more
in module 6)

Indentation: 1 level
2 levels

Module 3 : Basic syntax and semantics 23 of 34


3) Indentation
• Purpose:
- Python requires indentation for grouping, in
particular for control flow: branching and
looping (see module 6)
- Make code more readable

• Note: consistently use same number of spaces


(see more in module 6)

Module 3 : Basic syntax and semantics 24 of 34


4) Tokens
• Tokens are special elements in a programming
language (note: interpreter/compiler will first
identify them when parsing each statement in a
program, so that the interpreter/compiler can
later understand the meaning of your statement)
• In Python, we have four basic types of tokens:
• Keywords
• Operators
• Punctuators and Delimiters
• Literals
Module 3 : Basic syntax and semantics 25 of 34
Keywords
• Special words reserved in Python
• Programmers (we) cannot use them to name things

and del from not while


as elif global or with
assert else if pass yield
break except import print
class in raise
continue finally is return
def for lambda try

Note: “exec” removed in Python


Module 3 : Basic syntax and semantics 26 of 34 3
Operators
• Special characters (or sequence of characters)
that imply certain operations, e.g.,
mathematical and logical.

+ - * ** / // %
<< >> & | ^ ~
< > <= >= == !=

Note: < > removed in Python 3

Module 3 : Basic syntax and semantics 27 of 34


Punctuators & Delimiters
• Punctuators, also known as delimiters separate
different types of elements in Python
statements and expressions

‘ “ # \ _
( ) [ ] { } @
, : . ` = ;
+= -= *= /= //= %=
&= |= ^= >>= <<= **=

Module 3 : Basic syntax and semantics 28 of 34


Literals
• Literals are fixed values used in a computer
program, e.g., 123 and 3.14 are literals

E.g., 1, 2, and 3 in the program above are literals


How many numerical literals in program above?
Module 3 : Basic syntax and semantics 29 of 34
5) Expressions
• Anything that produces/returns a value
• Let’s say by combining values (e.g., literals, variables,
etc.) and operations (e.g., operators, functions, etc.)
• E.g.,
• 3.14
• 100 * 5
• result * 100
• 2 * math.pi * radius + float(input("input:"))
• Note:
• Interpreter ignores whitespaces (but helps readability)
• In Python, statements do not return a value

Module 3 : Basic syntax and semantics 30 of 34


More: Side Effects and Returns
• Make sure you get the difference:
What is the difference between a side effect
and a return?
• 1 + 2 returns a value (it’s an expression). You
can catch the return value. However, nothing
else changed as a result
• print("hello") doesn’t return anything, but
something else, the side effect, did happen.
Something printed on screen!
• How about a=1+2?
Module 3 : Basic syntax and semantics 31 of 34
6) Interpreter Errors
• The interpreter translates Python code into
machine language. The first stage of that process
is determining whether it is valid or not
• If the code is somehow malformed, Python cannot
run your code and you get an interpreter error:

Syntax error:
Python cannot
translate the code
Module 3 : Basic syntax and semantics 32 of 34
Take Home Messages
• Semantics – Meaning of your program
• Syntax – Specifying your algorithm using the
programming language
• This module is about terminologies and syntax:
Statements, statement continuation, modules,
comments, whitespace, indentation, tokens
(keywords, operators, punctuators and
delimiters, literals), functions, expression,
interpreter errors
• Side effects and returns (statement VS expression)

Module 3 : Basic syntax and semantics 33 of 34


Reading Assignment
• Textbook
Chapter 1: Beginnings
1.2 to 1.4

Module 3 : Basic syntax and semantics 34 of 34

You might also like