0% found this document useful (0 votes)
6 views

Comp Python Lecture 02

The document provides an overview of programming syntax in Python, covering topics such as comments, variables, naming rules, multiple assignments, output variables, keywords, syntax errors, and logical errors. It explains how to use comments for code documentation, the rules for naming variables, and the types of errors that can occur in Python programming. Additionally, it highlights the importance of understanding syntax and logical errors to ensure code runs correctly.

Uploaded by

danieldenis6060
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Comp Python Lecture 02

The document provides an overview of programming syntax in Python, covering topics such as comments, variables, naming rules, multiple assignments, output variables, keywords, syntax errors, and logical errors. It explains how to use comments for code documentation, the rules for naming variables, and the types of errors that can occur in Python programming. Additionally, it highlights the importance of understanding syntax and logical errors to ensure code runs correctly.

Uploaded by

danieldenis6060
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction to Computer

Programming (COMP-122)
Lecture 2: Programming Syntax

CSIT Department | MIT


Outline
+Comments
+Variables & Naming Rules
+Multiple Assignments
+Output Variables
+Python Keywords
+Syntax Errors
+Logical Errors

2
Comments
+Comments can be used to explain Python code.
+Comments can be used to make the code more readable.
+Comments can be used to prevent execution when testing code.
+Python has commenting capability for the purpose of in-code
documentation.
+Comments start with a #, and Python will render the rest of the line as
a comment:

#This is a comment.
print("Hello, World!")

3
Examples of Comments
+Example 1:
#This is a comment
print("Hello, World!")

+Example 2:
o Comments can be placed at the end of a line, and Python will ignore the rest of
the line:
print("Hello, World!") #This is a comment

+Example 3:
o A comment does not have to be text that explains the code, it can also be used to
prevent Python from executing code:
#print("Hello, World!")
print("Cheers, Mate!")

4
Multi Line Comments
+To add a multiline comment you use triple single quotes or
triple double quotes as shown below:
“““
This is a comment
written in
more than just one line
”””
print("Hello, World!")

5
Variables - Recap
+Variables are containers for storing data values.
+Python has no command for declaring a variable.
+A variable is created the moment you first assign a value to
it.

+Variables do not need to be declared with any particular


type, and can even change type after they have been set.

6
Variables Cont.…
+You can get the data type of a variable with the type()
function.

7
Variable Naming Rules
+A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
+Variable names are case-sensitive.
+For example, this will create two variables:
a = 4
A = "Sally"
#A will not overwrite a

8
Variable Naming Rules
+Rules for Python variables:
oA variable name must start with a letter or the underscore
character
oA variable name cannot start with a number
oA variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
oVariable names are case-sensitive (age, Age and AGE are
three different variables)

9
Variable Naming Rules
+Examples of legal variable names:

10
Multi-Words Variable Names
+Camel Case
o Each word, except the first, starts with a capital letter:
myVariableName = "John"
+Pascal Case
o Each word starts with a capital letter:
MyVariableName = "John"
+Snake Case
o Each word is separated by an underscore character:
my_variable_name = "John"

11
Multiple Assignments
+Many Values to Multiple Variables
o Python allows you to assign values to multiple variables in one line:
x, y, z = 1, 3, 6
print(x + y + z)
+One Value to Multiple Variables
o And you can assign the same value to multiple variables in one line:
x = y = z = "Orange"
print(x)
print(y)
print(z)

12
Output Variables
+The Python print statement is often used to output variables.
+To combine both text and a variable, Python uses the +
character for string concatenation:
x = "awesome"
print("Python is " + x)
+Or you can use a comma to achieve a similar result:
x = "Python is"
y = "awesome"
print(x, y)

13
Output Variables cont…
+For numbers, the + character works as a mathematical operator:
x = 5
y = 10
print(x + y)
+Note: If you try to combine a string and a number using + Python will
give you an error. However, you can use commas to print numbers and
strings together.

14
Python Keywords
+Keywords in Python are reserved words that can not be used as a
variable name, function name, or any other identifier.
+Some of the Keywords in Python:
and as assert break

if import in is

15
Python Keywords
+We can also get all the keyword names using the below
code:

16
Syntax Errors
+ Syntax errors are defined as violations of rules and regulations to form a
layout of a certain logic.
+ Example:
>>> print "hello"
SyntaxError: Missing parentheses in call to 'print'. Did
you mean print("hello")?
+ A program does not execute when there is a syntax error. On the other hand,
a program can stop its normal execution when it encounters a runtime
error. For example, ZeroDivisionError which occurs when one tries to
divide by zero.
+ Common kinds of syntax errors include:
o NameError
o TypeError
o IndentationError

17
NameError
+Raised when a variable is not found in the local or global
scope.
+When you try to use a variable or a function name that is not
valid.
+Possible causes:
o Misspelled variable or function name
o Calling a function before declaration
o Forget to define a variable
o Declaring a variable out of scope

18
TypeError
+Raised when a function or operation is applied to an object of an
incorrect type.
+For example, using the + (addition) operator on a string and an integer
value will raise TypeError.
+Possible causes:
o Unsupported operation between two data types
o Calling a non-callable identifier
o Incorrect type of list index

19
IndentationError
+Raised when there is an incorrect indentation.
+The indentation error can occur when the spaces or tabs are not
placed properly.
o The programmer is making use of both spaces and tabs in the code
written. This creates ambiguity, and the interpreter is unable to
determine which item we need to use in the first place as both are
being used interchangeably.
o The programmer missed upon indentation for compound
statements like if, for, while, etc.

20
Logical Errors
+Also called semantic errors, logical errors cause the program to
behave incorrectly. Unlike a program with syntax errors which cannot
run at all or a program with runtime errors which crushes while
running, a program with logic errors can be run but it does not operate
as intended. Consider the following example of a logical error:

+This program’s logic is incorrect. To correct it, line 3 should read:


z = (x+y)/2

21
End.

22

You might also like