Dcit 22 Lesson 03 Basic Syntax of Python Programming Language
Dcit 22 Lesson 03 Basic Syntax of Python Programming Language
“BASIC SYNTAX”
OBJECTIVES:
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
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 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.
• 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
• Simplicity – as mentioned earlier, the language itself is very readable, and isn’t too
complex. You can learn Python in no time!
2|Page
PYTHON IDENTIFIERS
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
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.
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
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 –
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
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.
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
EXAMPLE 2:
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
int (signed integers), long (long integers, they can also be represented in octal and
hexadecimal), float (floating point real values) and complex (complex numbers)
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
8|Page
These operators compare the values on either sides of them and decide the relation among
them. They are also called Relational operators.
3. Logical Operator
9|Page