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

Lecture 2 - Programming Syntax

The document provides an overview of Python programming syntax, covering topics such as comments, variables, naming rules, multiple assignments, keywords, and syntax errors. It explains how to use comments for documentation, the rules for naming variables, and the types of syntax errors that can occur in Python. Additionally, it discusses logical errors and their impact on program behavior.

Uploaded by

wmkandawire648
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 2 - Programming Syntax

The document provides an overview of Python programming syntax, covering topics such as comments, variables, naming rules, multiple assignments, keywords, and syntax errors. It explains how to use comments for documentation, the rules for naming variables, and the types of syntax errors that can occur in Python. Additionally, it discusses logical errors and their impact on program behavior.

Uploaded by

wmkandawire648
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture 2:

Programming Syntax
Outline
• Comments
• Variables & Naming Rules
• Multiple Assignments
• Python Keywords
• Syntax Errors
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!")
Examples of Comments
• Example 1:
#This is a comment
print("Hello, World!")

• Example 2:
• 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:
• 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!")
Multi Line Comments
• Python does not really have a syntax for multi line comments.
• To add a multiline comment you could insert a # for each line:
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Variables
• 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.
x=5
y = "John"
print(x)
print(y)
• Variables do not need to be declared with any particular type,
and can even change type after they have been set.
Variables Cont…
• You can get the data type of a variable with the type() function.
x=5
y = "John"
print(type(x))
print(type(y))
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
Variable Naming Rules
• Rules for Python variables:
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three
different variables)
Variable Naming Rules
• Examples of legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Multi Words Variable Names
• Camel Case
Each word, except the first, starts with a capital letter:
myVariableName = "John”
• Pascal Case
Each word starts with a capital letter:
MyVariableName = "John”
• Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"
Output Variables
• The Python print statement is often used to output variables.
• To combine both text and a variable, Python uses the + character:
x = "awesome"
print("Python is " + x)
• You can also use the + character to add a variable to another variable:
x = "Python is "
y = "awesome"
z= x+y
print(z)
Multiple Assignments
• Many Values to Multiple Variables
Python allows you to assign values to multiple variables in one line:
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
• One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
x = y = z = "Orange"
print(x)
print(y)
print(z)
Multiple Assignments
• If you have a collection of values in a list, tuple etc. Python
allows you extract the values into variables. This is
called unpacking.
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Output Variables
• 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, Python will
give you an error.
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 inis
Python Keywords
• We can also get all the keyword names using the below code.
import keyword
print("The list of keywords is : ")
print(keyword.kwlist)
Syntax Errors
• Syntax errors are defined as violations of rules and regulations to form a
layout of a certain logic.
• Raised by the parser when a syntax error is encountered.
• Example:
>>> print "hello"
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?
• Many times though, a program results in an error after it is run even if it
doesn't have any syntax error. Such an error is a runtime error, called an
exception. A number of built-in exceptions are defined in the Python library.
• Some of the errors types are:
• NameError
• TypeError and
• IndentationError
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.
• Causes:
• Misspelled Variable or Function Name
• Calling a Function Before Declaration
• Forget to Define a Variable
• Try to Print a Single Word
• Declaring a Variable Out of Scope
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.
• Causes:
• Unsupported operation between two data types
• Calling a non-callable identifier
• Incorrect type of list index
IndentationError
• Raised when there is an incorrect indentation.
• The indentation error can occur when the spaces or tabs are not
placed properly.
• 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.
• The programmer missed upon indentation for compound statements
like if, for, while, etc.
Logical Errors
• Also called semantic errors, logical errors cause the program to
behave incorrectly, but they do not usually crash the program.
Unlike a program with syntax errors, a program with logic errors
can be run, but it does not operate as intended. Consider the
following example of an logical error:
x = float(input('Enter a number: '))
y = float(input('Enter a number: '))
z = x+y/2
print ('The average of the two numbers you have entered is:',z)
QUESTIONS ???

You might also like