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

Dcit 22 Lesson 03 Basic Syntax of Python Programming Language

Uploaded by

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

Dcit 22 Lesson 03 Basic Syntax of Python Programming Language

Uploaded by

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

LESSON 03:

“BASIC SYNTAX”

OBJECTIVES:

After the completion of the chapter, students should be able to:


1. Discuss the basics of programming, and its related components.

DEVELOPMENT TOOLS

Interpreters
- like a compiler that translates higher level source code into target code (usually
machine language)

Debuggers
- allows a programmer to more easily trace a program’s execution in order to locate and
correct errors in the program’s implementation

Profilers
- collects statistics about a program’s execution allowing developers to tune appropriate
parts of the program to improve its overall performance

IDE
- an IDE (Integrated Development Environment)
- It provides different tools to computer programmers for software development.
- It usually comes with a source code editor and a debugger, among other features.

Console
- The console whowsx the code executed and “enables executing Python commands
and scripts line by line.”
- This is similar to your experience with Proramix or Google Collab
- An error message will also appear in the console if your code has an error.

Syntax
- A set of rules that dictate how to use characters and keywords for the computer to
interpret.
- This includes how the symbols, punctuation, and words are used in a programming
language.
- Just like when learning a foreign language, not following these riles will result in error.

WHAT IS PROGRAMMING?

Programming is the act of giving very specific instructions for a computer to follow. Human
beings are the puppet masters controlling a computer’s every move, so instructions must be
very specific. Often, it must be repeated and rewritten until the computer gets it right. To tell
your computer what to do, you will need to speak its language. This is where programming
languages come in.

Programming Languages translate your command into machine code to make it easier for
your computer to understand you. Think of it as an interpreter between you and a person
speaking a foreign language. There are a lot of programming languages you can choose from.

1|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


Defensing on what tasks you want to accomplish. This language can be categorized into two
types: the low level and the high level.

Low Level Language are:


• Not programmer friendly
• Hard to understand
• Complex to debug
• Not commonly used
• Examples: Assembly Language, C etc.

Low level languages are closer to machine code compared to high level languages and are
not very programmer-friendly. They are also not as commonly used as high level languages.

High Level Language are:


• Programmer-friendly
• Simpler to understand
• Relatively easy to debug
• Widely used
• Examples” Java, C++, C#, JavaScript, Python, etc.

High level languages are much simpler to understand and programmer-friendly. They are
also widely used these days.

WHAT IS PYTHON?

Python
- general-purpose interpreted, interactive, object-oriented, and high-level programming
language.
- Develop in the late 1980s
- Created by Guido van Rossum in the 1980s, it is considered to be a high-level
language that is known for its readability.
- It’s very easy to learn and read and is a very popular programming language.
- Python is Interpreted − Python is processed at runtime by the interpreter. You do not
need to compile your program before executing it. This is similar to PERL and PHP.
- Python is Interactive − You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
- Python is Object-Oriented − Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
- Python is a Beginner's Language − Python is a great language for the beginner-
level programmers and supports the development of a wide range of applications from
simple text processing to WWW browsers to games.

WHAT DO I NEED TO KNOW ABOUT THE SYNTAX OF PYTHON?

• Designed for readability – the reason why python is easy to learn is because it was
designed for readability
• Utilized the English language – it basically uses English words to make commands.
• Only needs to move to the next line to complete a code

WHY SHOULD YOU LEARN PYTHON?

• Simplicity – as mentioned earlier, the language itself is very readable, and isn’t too
complex. You can learn Python in no time!
2|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


• Versatility – It can be used to develop websites and apps, do data analysis, and it can
also be used for automation.
• Career opportunities and growth – Python is largely in demand. Knowing a thing or
two about this programming language can help you land a good job.

PYTHON IDENTIFIERS

- A Python identifier is a name used to identify a variable, function, class, module or


other object.
- Python does not allow punctuation characters such as @, $, and % within identifiers.
Python is a case sensitive programming language. Thus, Manpower and manpower
are two different identifiers in Python.

Here are naming conventions for Python identifiers


• Class names start with an uppercase letter. All other identifiers start with a lowercase
letter.
• Starting an identifier with a single leading underscore indicates that the identifier is
private.
• Starting an identifier with two leading underscores indicates a strongly private
identifier.
• If the identifier also ends with two trailing underscores, the identifier is a language-
defined special name.

RESERVED WORDS

The following list shows the Python keywords. These are reserved words, and you cannot use
them as constant or variable or any other identifier names. All the Python keywords contain
lowercase letters only.

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

PRINT () FUNCTION

PRINT () FUNCTION
- The print () Function prints the text within the parentheses on the console. You can
input other text using this function.
- Outputs what is within the parentheses to the console

INPUT () FUNCTION

INPUT ()
- The input function allows us to ask users for some text or data input.
- Users can input data and out program will store it for them

3|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


- It pauses your program and waits for the user to enter some text of data. Once Python
receives the user’s input, it assigns that input to a variable to make it convenient for
you to work with

LINES AND INDENTATION

Indentation
- Is extremely important because we use them to create new block of code. In Java
code, for example, you can set a new block of code using curly braces. In Python, all
it takes is a bit of whitespace, or the space around your code.
- If you want indents, simply press the spacebar repeatedly, or use the tab key.
- Indented blocks are also proceeded by a colon on the previous line. In Java or C++,
you need to add a semicolon to end your statements. This isn’t necessary for Python,
because simply ending of code lets you end the stamen.

However, the following block of codes generates an ERROR

QUOTATION AND PARENTHESES IN PYTHON

Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as
long as the same type of quote starts and ends the string.

The triple quotes are used to span the string across multiple lines. For example, all the
following are legal −

Parenthesis “()”
- Indicate that a function is being called.
- In the print() function indicate that we need to put something within them
- They can also be used in mathematical operation, which we will be discussing in a
future tutorial

COMMENTS IN PYTHON
4|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


Comments
- Are completely ignored
- They’re entirely for the use of programmers like you.
- Can be written using the hash character (#)
- Are not read in by the interpreter, meaning that you can write whatever you want, and
the computer will ignore it

A hash sign (#) that is not inside a string literal begins a comment. All characters after the #
and up to the end of the physical line are part of the comment and the Python interpreter
ignores them.

You can type a comment on the same line after a statement or expression –

You can comment multiple lines as follows –

Following triple-quoted string is also ignored by Python interpreter and can be used as a
multiline comments:

VARIABLES

Think of variables as boxes. Imagine that you’re moving out of your parent’s house. You’ll
have to pack all your stuff into boxes. Some of them contains shoes, or books, or clothes you
should have thrown out years ago. Variables are kind of like that, to avoid cluttered text editor,
a single box can contain a numerical value, a string of just one word, or the first ten pages of
the hobbit.

Variables
5|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


- are containers for data
- Can be named almost anything but the first character cannot be a digit
- Equivalent to a certain value (a string or number)
- Are used to save values into memory using a name that we assign
o Value can be anything, from a string to number, etc.
- Variables are nothing but reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory.
- Based on the data type of a variable, the interpreter allocates memory and decides
what can be stored in the reserved memory. Therefore, by assigning different data
types to variables, you can store integers, decimals or characters in these variables.

How Do We Declare Variables?

In Python, there is no specific command or keyword for creating a variable. You simply assign
a value to something then print it.

The ‘x’ and ‘y’ here are what we’d call variable. We assigned the values of a number and a
string to x and y.

Assigning Values to Variables

Multiple Assignment - Python allows you to assign a single value to several variables
simultaneously.

Here, an integer object is created with the value 1, and all three variables are assigned to the
same memory location. You can also assign multiple objects to multiple variables.

6|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


EXAMPLE 1:

EXAMPLE 2:

DATA TYPE

What is Data Type?

The data stored in memory can be of many types. Python has various standard data types
that are used to define the operations possible on them and the storage method for each of
them.

7|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


Python has five standard data types:
• Numbers
• String
• List
• Tuple
• Dictionary

What are Data Types(Numbers)?

int (signed integers), long (long integers, they can also be represented in octal and
hexadecimal), float (floating point real values) and complex (complex numbers)

What are Data Types(String)?

Strings in Python are identified as a contiguous set of characters represented in the quotation
marks. Python allows for either pairs of single or double quotes. The plus (+) sign is the string
concatenation operator and the asterisk (*) is the repetition operator.

COMMON OPERATORS

1. Arithmetic Operators

Operator Description Example


+ Addition Adds values on either side of the operator. a + b = 30
Subtracts right hand operand from left hand
- Subtraction a – b = -10
operand.
* Multiplication Multiplies values on either side of the operator a * b = 200
/ Division Divides left hand operand by right hand operand b/a=2
Divides left hand operand by right hand operand and
% Modulus b%a=0
returns remainder

8|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS


Performs exponential (power) calculation on a**b =10 to the
** Exponent
operators power 20
Floor Division - The division of operands where the
9//2 = 4 and
result is the quotient in which the digits after the
9.0//2.0 = 4.0, -
// decimal point are removed. But if one of the
11//3 = -4, -
operands is negative, the result is floored, i.e.,
11.0//3 = -4.0
rounded away from zero (towards negative infinity) −

2. Comparison Operator / Relationship Operator

These operators compare the values on either sides of them and decide the relation among
them. They are also called Relational operators.

Operator Description Example


If the values of two operands are equal, then the
== (a == b) is not true.
condition becomes true.
If values of two operands are not equal, then
!= (a != b) is true.
condition becomes true.
If values of two operands are not equal, then (a <> b) is true. This is
<>
condition becomes true. similar to != operator.
If the value of left operand is greater than the value
> (a > b) is not true.
of right operand, then condition becomes true.
If the value of left operand is less than the value of
< (a < b) is true.
right operand, then condition becomes true.
If the value of left operand is greater than or equal to
>= the value of right operand, then condition becomes (a >= b) is not true.
true.
If the value of left operand is less than or equal to
<= the value of right operand, then condition becomes (a <= b) is true.
true.

3. Logical Operator

Operator Description Example


and Logical If both the operands are true then condition (a and b) is true.
AND becomes true.
or Logical OR If any of the two operands are non-zero then (a or b) is true.
condition becomes true.
not Logical Used to reverse the logical state of its operand. Not(a and b) is
NOT false.

9|Page

DCIT 22 – COMPUTER PROGRAMMING 1 PREPARED BY: EDISON S. DE LOS SANTOS

You might also like