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

Lesson 3 Program Statements Variables and Expressions (1)

This document is an introduction to Python programming, focusing on program statements, variables, and expressions. It covers data types, variable assignment, keywords, operators, and the importance of comments in code. The lesson aims to equip candidates with the skills to explain and apply these concepts effectively in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lesson 3 Program Statements Variables and Expressions (1)

This document is an introduction to Python programming, focusing on program statements, variables, and expressions. It covers data types, variable assignment, keywords, operators, and the importance of comments in code. The lesson aims to equip candidates with the skills to explain and apply these concepts effectively in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to Python

Programming

Lesson 3:
Program Statements, Variables and Expressions
Lesson objectives

Upon successful completion of this lesson, candidates will be able to demonstrate


their competence in, and their ability to:
1. explain identifiers, variable, constants, assignment and expressions used in
Python.
2. identify basic concepts of input and output .
3. apply string manipulation techniques in python.
Datatypes and their values

 In any programming language we find values of different data types. Value is an important
concept in programming. It could be a letter, a string of characters or a number.
 There are different types of values such as:
Integers (int):
>>> x = 1
Floating point values (float):
>>> x = 4.3
Complex values (complex):
>>> x = complex(4., -1.)
String:
>>> x = "Hello World"
 Python supports all primary data types such as integers, floating point numbers and strings.
Other than those, it has built-in support for data types such as lists, tuples, dictionaries and
complex numbers.

 Python interpreter can find the type of a given value using ‘type’ keyword.

 Python supports a number of built-in types and operations. Though different languages may
handle variable types uniquely, most are quite similar. Python variable types, like the rest of
the language, are quite simple.

 Variable types are determined by the value stored within the variable. Unlike most other
languages, keywords like "int", "String", or "bool" are not required in Python, as Python
supports type inferencing. The most general compatible variable type is chosen.
Variables in Python
 A variable is something that holds a value which may change later. In simplest terms, a variable
is just a box that you can put stuff in. You can use variables to store all kinds of stuff, but for now,
we are just going to look at storing numbers in variables.
 Ability to manipulate variables is a versatile feature in any programming language.
 Use of assignment statement (‘= ‘ operator) in Pthon, creates new variables and gives them
values as well:
>>> student_name = ‘John Buns'
>>> student_no = 5
>>> student_average = 47.561
 The first assigns a string to a new variable named student_name.
 The second gives the integer 5 to a new variable named student_no.
 The third assigns an approximate value for student_average.
Differentiating variable names and keywords

 Unlike many other high level languages, variable types are not declared before assigning
values. The type of a variable is decided by the type of the value it is initialized or later refers
to
 Generally, any programmer is taught to choose meaningful names for variables which helps
human readers to understand what they are used for. You may construct variable names to
represent what the variable is used for.
 Variable names can be quite long and can have both English letters and numbers. However,
the variable name must start with a letter. It is advised to start with a lowercase letter. The
underscore character “_” can appear in a name where it can connect multiple names such as
student_name and student_number. Variable names are case sensitive. Case sensitivity
example: myVariable, myvariable, and Myvariable represent three separate variable names.
Keywords

 Keywords define the language’s syntax rules and structure, and they cannot be used as
variable names.
 There are 31 keywords in Python version 2 as shown below.
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
Operators, Operands and Expressions
 Symbols that represent some actions are called Operators.
 The action could be addition of two numbers.
 The operator performs an action on operands such as two numbers.
 The operators use +, -, *, / and % to perform computations Addition, Subtraction,
Multiplication, Division and Modulus.
>>> x = 2
>>> y = 3
>>> z = 5
>>>x * y 6
>>>x * y + z 11
>>> (x + y) * z 25
 In programming, an expression is any legal combination of symbols that represents a
value. The expression is consists with values, variables, and operators. In the domain of
computing, expressions are written by developers, interpreted by computers and
evaluated.
 In the expression,
x+3
x and 3 are operands
+ is an operator

 An expression does not necessarily do anything, it evaluates to, that is, reflects, a value.
For example, 3 is an expression which evaluates to 3. Following are examples for
expressions assuming that the variables has been assigned a particular value.
8888
x
x + 88

 One physical line of code is considered as one statement which does something. Most
often, one physical line of code will correspond to one statement. Within a statement you
can find expressions. A statement is an instruction that the Python interpreter can execute.
Interactive mode and script mode

There are certain advantages that can be achieved with an interpreted language
compared to compiled one. Here, you can test line by line in interactive mode before
you write few lines together in a script. However, there are some differences with
Interactive and Script mode.

If same lines are typed into a script and executed, you cannot expect the same output.
That is because these expressions have no visible effect. However, Python does
evaluate all statements but do not display result unless asked with a print statement.
Order of operations

In mathematics and computer programming, the order of operations (or operator precedence) is
a collection of rules that reflect conventions about which procedures to perform first in order to
evaluate a given mathematical expression. Python uses the standard order of operations as
taught in Algebra and Geometry classes at high school or secondary school. That is,
mathematical expressions are evaluated in the following order (memorized by many as
PEMDAS), which is also applied to parentheticals.

Note that operations which share a table row are performed from left to right. That is, a division
to the left of a multiplication, with no parentheses between them, is performed before the
multiplication simply because it is to the left.
The table shows the operator precedence in Python
from lowest to highest. Operators in the same box have
the same precedence. Unless the syntax is explicitly
given, operators are binary. Operators in the same box
group left to right (except for exponentiation, which
groups from right to left).
Comments in Programs
There will always be a time in which you have to return to your code. Perhaps it is to fix a bug, or to add a
new feature. Regardless, looking at your own code after six months is almost as bad as looking at someone
else's code. What one needs is a means to leave reminders to yourself as to what you were doing.
For this purpose, you leave comments. Comments are little snippets of text embedded inside your code that
are ignored by the Python interpreter. The natural language can be used for commenting the code. It can
appear anywhere in the source code where whitespaces are allowed. It is useful for explaining what the
source code does by:
 explaining the adopted technical choice: why this given algorithm and not another, why calling this given
method
 explaining what should be done in the next steps (the TODO list): improvement, issue to fix
 giving the required explanation to understand the code and be able to update it yourself later or by other
developers
 It can also be used to make the compiler ignore a portion of code: temporary code for debugging and code
under development.
The following guidelines are from PEP 8 (Index of Python Enhancement Proposal), written by Guido
van Rossum.
1. Comments that contradict the code are worse than no comments. Always make a priority of
keeping the comments up-to-date when the code changes!
2. Comments should be complete sentences. The first word should be capitalized, unless it is an
identifier that begins with a lower case letter (never alter the case of identifiers!).
3. Block comments generally consist of one or more paragraphs built out of complete sentences,
with each sentence ending in a period.
4. You should use two spaces after a sentence-ending period in multi- sentence comments, except
after the final sentence.
5. Python coders from non-English speaking countries: please write your comments in English,
unless you are 120% sure that the code will never be read by people who don't speak your
language.
 Block comments generally apply to some (or all) code that follows them, and are
indented to the same level as that code. Each line of a block comment starts with a
# and a single space (unless it is indented text inside the comment). Paragraphs
inside a block comment are separated by a line containing a single #.

 Use inline comments sparingly. An inline comment is a comment on the same line
as a statement. Inline comments should be separated by at least two spaces from
the statement. They should start with a # and a single space.

 Inline comments are unnecessary and in fact distracting if they state the obvious.
The End!!!
Compiled by F. Zinyowera

You might also like