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

4 - Lectures Note Week5 Fundamentals of Programming

Uploaded by

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

4 - Lectures Note Week5 Fundamentals of Programming

Uploaded by

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

CSC120: Computer as a Problem-Solving

Tool
Topic 4:
Fundamentals of Programming (1)
Lecture 4 Week5:
Lecturer
Dr. Ufuoma C. Ogude

Department of Computer Sciences


University of Lagos
1
Outlines of the Course Wk2-
7
 Concept of Problem solving
 Introduction to programming
 Concept & properties of Algorithm
 Fundamentals of programming (1): variable/variable naming
convention/data types in Python
 Fundamentals of programming (2): operators and expressions
 Fundamentals of programming (3): control structures

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

greater programming community.


Python
 Python is an object-oriented programming language created
by Guido Rossum in 1989.
 It is ideally designed for rapid prototyping of complex
applications.
 It has interfaces to many OS system calls and libraries and
is extensible to C or C++.
 Many large companies use the Python programming language,
including NASA, Google, YouTube, BitTorrent, etc.

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.

 Middle level language


Python is also the go-to choice for the creation of machine learning
models and artificial intelligence.

 Efficient and fast


Python is one of the fastest-growing and most in demand computer
programming skills.

11
Write a Python
Program
 Write your first Python program

 Learn what happens when you run a program with an error

 Learn how to declare a variable and inspect its value

 Learn how to write comments

 Using the interactive window, display some text using print().

 Using the interactive window, assign a string literal to a variable.

 Then print the contents of the variable using the print() function.
12

 Repeat the first two exercises using the editor window.


Fundamentals of Python Programming
 The Context of Software Development
 Values and Variables
 Expressions and Arithmetic
 Conditional Execution
 Iteration, Using Functions
 Writing Functions, More on Functions
 Objects, lists
 Tuples, Dictionaries, and Sets
 Handling Exceptions, Custom Types
 Class Design: Composition and Inheritance
13
Python Syntax
 Python syntax refers to the set of rules that defines

how human users and the system should write and

interpret a Python program.

 If you want to write and run your program in Python,

you must familiarize yourself with its syntax.

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,

variable, module, or other objects that you’ll be using in your

Python program.

 Any entity you’ll be using in Python should be appropriately

named or identified as they will form part of your program.

16
Naming Conventions
 Here are Python naming conventions that you should be
aware of:

 An identifier can be a combination of uppercase letters,

lowercase letters, underscores, and digits (0-9).

Hence, the following are valid identifiers:

myClass, my_variable, var_1, and print_hello_world.

 Special characters such as %, @, and $ are not allowed

within identifiers.
17
Naming Conventions
 An identifier should not begin with a number.

Hence, 2variable is not valid, but variable2 is acceptable.

 Python is a case-sensitive language and this behavior

extends to identifiers.

Thus, Labor and labor are two distinct identifiers in

Python.

 You cannot use Python keywords as identifiers.

18
Naming Conventions
 Class identifiers begin with an uppercase letter, but the

rest of the identifiers begin in lowercase.

 You can use underscores to separate multiple words in

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

indicate string literals.


 You can use single, double, or triple quotes but you must

start and end the string with the same type.

 You would use the triple quotes when your string runs

across several lines.

20
Python Statements
 Statements are instructions that a Python
interpreter can execute.
When you assign a value to a variable, say my_variable = “dog”,

you’re making an assignment statement.

An assignment statement may also be as short as c = 3.

 There are other kinds of statements in Python, like

if statements, while statements, for statements,

etc. 21
Multi-line Statements

 A statement may span over several lines.


To break a long statement over multiple lines, you can
wrap the expression inside parentheses, braces, and
brackets.
This is the preferred style for handling multi-line
expressions.
 Another way to wrap multiple lines is by using a
backslash (\) at the end of every line to indicate line
continuation.
22
Variables
 Variables are containers for storing data values

that can be access or change.

 It is a way of pointing to a memory location used by

a program.

 Variables are used to instruct the computer to save

or retrieve data to and from memory location.

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

Java, C, or C++ when it comes to dealing with variables.

Other languages declare and bind a variable to a

specific data type.

 This means that it can only store a unique data type.

Hence, if a variable is of integer type, you can only save

integers in that variable when running your program.


27
Creating Variables
 Python has no command for declaring a variable
Python is a lot more flexible when it comes to handling
variables.
 A variable is created the moment you first assign a value
to it.
If you need a variable, think of a name and declare it by
assigning a value.
 Variables do not need to be declared with any
particular type and can even change type after they have
been set.
If you need to, you can change the value and data type 28
Assignment Operator

 An operator is a symbol, such as +, that performs an operation

on one or more values.

For example, the + operator takes two numbers, one to the left

of the operator and one to the right, and adds them together.

 Values are assigned to variable names using a special symbol

called the assignment operator (=) .

The = operator takes the value to the right of the operator and

assigns it to the name on the left. 29


Example
x=5
y = "John"
print(x)
print(y)

x=4 # x is of type int


x = "Sally" # x is now of type str
print(x)

Print the data type of the variable x


x=5
print(type(x))
Answer: <class “int”>

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

the variable to “yellow”:

>>>my_variable = “yellow”

To see what’s currently store in my_variable, use the print command:

>>>print(my_variable)

On the next line, you’ll see:

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

programmers and application developers for workable data.

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 −

 int (signed integers)

 long (long integers, they can also be

represented in octal and hexadecimal)

 float (floating point real values)

 complex (complex numbers


38
Example

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.

 Using the interactive window, display some text using print().

 Using the interactive window, assign a string literal to a variable.

 Then print the contents of the variable using the print() function.

 Repeat the first two exercises using the editor window.

41
Arithmetic Operators

42
Numeric Operations

43

You might also like