4 - Lectures Note Week5 Fundamentals of Programming
4 - Lectures Note Week5 Fundamentals of Programming
Tool
Topic 4:
Fundamentals of Programming (1)
Lecture 4 Week5:
Lecturer
Dr. Ufuoma C. Ogude
2
Fundamentals of Programming
(1)
Variables and Data Types in Python
3
Python
Python is a Scripting programming language known for both its
simplicity and wide breadth of applications.
For this reason it is considered one of the best languages for
beginners.
Python is a powerful and flexible programming language.
It uses concise and easy-to-learn syntax which enables
programmers to write more codes and develop more complex
programs in a much shorter time.
Used for everything from Web Development to Scientific
Computing
Python is referred to as a “general purpose” language by the
4
5
6
7
8
9
Why Python
Robust Language
Despite its simplicity, Python is also robust enough to carry out
sophisticated scientific and mathematical tasks.
Highly portable
Python is supported by many different operating systems, including
Windows, Mac, and Linux platforms.
Structured Language
Python is quite versatile in the sense that it supports both
structured programming and object-oriented programming
approaches.
It allows the use of certain aspects of functional programming. 10
Why Python
Rich system library
Python comes with a robust standard library of modules, functions,
and tools.
11
Write a Python
Program
Write your first Python program
Then print the contents of the variable using the print() function.
12
14
Keywords
Python keywords are reserved words in Python that should
not be used as variable, constant, function name, or identifier
in your code.
Take note of these keywords if you don’t want to run into
errors when you execute your program:
and assert break class
continue def del elif
else except exec finally
for from global if
import in is lambda
not or pass print
raise return try while
with yield
15
A Python Identifier is a name given to a function, class,
Python program.
16
Naming Conventions
Here are Python naming conventions that you should be
aware of:
within identifiers.
17
Naming Conventions
An identifier should not begin with a number.
extends to identifiers.
Python.
18
Naming Conventions
Class identifiers begin with an uppercase letter, but the
your identifier.
You should always choose identifiers that will make
sense to you even after a long gap.
Hence, while it is easy to set your variable to c = 2,
you might find it more helpful for future reference
if you use a longer but more relevant variable name
19
such as count = 2.
Using Quotations
Python allows the use of quotation marks to
You would use the triple quotes when your string runs
20
Python Statements
Statements are instructions that a Python
interpreter can execute.
When you assign a value to a variable, say my_variable = “dog”,
etc. 21
Multi-line Statements
a program.
23
Rules to follow when naming variables:
Variable names can contain letters, numbers, and the
underscore.
Variable names cannot contain spaces.
Variable names cannot start with a number.
Case matters—for instance, temp and Temp are different.
It helps make program more understandable if you choose
names that are descriptive, but not so long that they clutter
up your program.
A variable can have a short name (like x and y) or more
24
descriptive name (age, carname, total_volume)
Python
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)
25
Note: Remember that variable names are case – sensitive
Example
Examples of Legal Variable names:
myvar = “John” MYVAR = “John”
my_var = “John” myVar2 = “John”
_my_var = “John”
myVar = “John”
Examples of Illegal Variable names:
2myvar = “John”
my – var = “John”
my var = “John” 26
NOTE
Python differs significantly from languages such as
For example, the + operator takes two numbers, one to the left
of the operator and one to the right, and adds them together.
The = operator takes the value to the right of the operator and
x = str(83)
print(x[0])
print(x[1])
y = int(x) 30
print(y)
Example
To illustrate these features:
In Python, you declare a variable by giving it a value:
my_variable = 10
Take note that when you are declaring a variable, you are not stating
that the variable my_variable is equal to 10.
What the statement actually means is “my_variable is set to 10”.
To increase the value of the variable, you can enter this statement on
the command line:
>>>my_variable = my_variable + 3
To see how Python responded to your statement, invoke the print
command with this statement:
>>>print(my_variable)
You’ll see this result on the next line: 31
13
Practice
To use my_variable to store a literal string “yellow”, you’ll simply set
>>>my_variable = “yellow”
>>>print(my_variable)
yellow
32
Output Variables
The Python print statement is often used to output variables.
To combine both text and a variable, Python uses the + character:
Example
x = "awesome"
print ("Python is " + x)
You can also use the + character to add a variable to another
variable:
Example
x = "Python is "
y = "awesome"
z=x+y 33
print(z)
Output Variables
For numbers, the + character works as a mathematical operator:
Example
x=5
y = 10
print (x + y)
If you try to combine a string and a number, Python will give you
an error:
Example
x=5
y = "John"
print (x + y) 34
Data Types
The data stored in memory can be of many types.
For example,
a person's age is stored as a numeric value and
the address is stored as alphanumeric characters.
Python has various standard data types that are used to
define the operations possible on them and the storage
method for each of them.
35
Data Types
Python handles several data types to facilitate the needs of
These include:
strings,
numbers,
Booleans,
lists,
date, and
time.
36
Standard Data Types
In programming data types is an important concept.
Variable can store data of different types and different types
can do different things.
Python has the following data types built – in by default in the
categories:
Text type: str
Numeric types: int, float, complex
Sequence types: list, type, range
Mapping types: dictionary
Set types: set, frozenset
Boolean types: bool
37
Binary types: bytes, bytearray, memoryview
Python Numbers
Python supports four different numerical types −
39
Summary
You wrote and executed your first Python program!
You wrote a small program that displays the text "Hello, World"
using the print() function.
Then you learned about syntax errors, which occur before IDLE
executes a program that contains invalid Python code, and runtime
errors, which only occur while a program is running.
You saw how to assign values to variables using the assignment
operator (=) and how to inspect variables in the interactive
window.
Finally, you learned how to write helpful comments in your code
for when you or someone else looks at it in the future. 40
Review Exercises
Write a program that IDLE won’t run because it has a syntax error.
Write a program that crashes only while it’s running because it has a
runtime error.
Then print the contents of the variable using the print() function.
41
Arithmetic Operators
42
Numeric Operations
43