Uploaded by Malvin Seth Oduro

Session 7

advertisement
DCIT 201- Programming I
Session 7 – Introduction to Python
Lecturer: Mr. Paul Nii Tackie Ammah, DCS
Contact Information: pammah@ug.edu.gh
Department of Computer Science
School of Physical and Mathematical Sciences
Session Overview
• Goals and Objectives
– Learn the basic concepts of Python
– Learn how to use Python interactively and by using a script.
Slide 2
Session Outline
•
•
•
•
•
•
•
What is Python?
Variable & Types
Comparison Operators
Assignment Operators
Logical Operators
Identity Operators
Membership Operators
Slide 3
What is Python?
• Python is a popular programming language.
• It was created by Guido van Rossum, and released in 1991.
• It is used for:
–
–
–
–
web development (server-side),
software development,
mathematics,
system scripting.
Slide 4
Why Python?
• Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs with fewer lines than
some other programming languages.
• Python runs on an interpreter system, meaning that code can be executed as soon
as it is written.
• Python can be treated in a procedural way, an object-oriented way or a functional
way.
Slide 5
Hello Python
• Open source and Free
• Innumerable packages
• Common Versions: Python 2.7 and Python 3.x
– Just syntactical differences
• We focus on python 3. Version 2 is deprecated as of 1st January, 2020
Slide 6
Installing Python
• Many PCs and Macs will have python already installed.
• To check if you have python installed on a Windows PC, search in the
start bar for Python or run the following on the Command Line
(cmd.exe):
>> python --version
• You can also visit https://www.python.org/downloads to download
different versions of python
Slide 7
Python Usage
• Two approaches
– Python shell, used from the terminal or prompt: ipython
– Python scripts: text files that have the .py extension
Slide 8
Python Indentation
• Indentation refers to the spaces at the beginning of a code line.
• Where in other programming languages the indentation in code is for
readability only, the indentation in Python is very important.
• Python uses indentation to indicate a block of code.
Slide 9
Python Variables & Comments
• In Python, variables are created when you assign a value to it
– e.g. height = 45
• Python variable names are case sensitive
• Comments start with a #, and Python will render the rest of the line as a
comment:
Slide 10
Multi-Line Comments
• To add a multiline comment you could insert a # for each line
OR
• you can use a multiline string (triple quotes) in your code, and place
your comment inside it
Slide 11
Assigning Multiple Values
• Python allows you to assign values to multiple variables in one line
• And you can assign the same value to multiple variables in one line
Slide 12
Python Data Types
• Python has the following data types built-in by default, in these
categories:
–
–
–
–
–
–
–
Text type: str
Numeric types: int, float, complex
Sequence types: list, tuple, range
Mapping type: dict
Set types: set, frozenset
Boolean types: bool
Binary types: byte, bytearray, memoryview
Slide 13
Python Type Examples
• Python Types
– Mapping types: dict
• x = {"name" : "John", "age" : 36}
– Set types: set, frozenset
• x = {"apple", "banana", "cherry"}
– Boolean types: bool
• ex = True
– Binary types: bytes, bytearray, memoryview
• x = b"Hello”
• x = bytearray(5)
• x = memoryview(bytes(5))
Slide 14
Python Type Examples
• Python Types
– Can be checked by using the type() function
– Text types: str
• x = "Hello World”
– Numeric types: int , float , complex
• x=20
• x=20.5
• x= 1j
– Sequence types: list , tuple , range
• x = ["apple", "banana", "cherry"]
• x = ("apple", "banana", "cherry"),
• x = range(6)
Slide 15
Casting
• If you want to specify the data type of a variable, this can be done with
casting.
• You can get the data type of a variable with the type() function
Slide 16
Python Operators
• Python divides the operators in the following groups:
–
–
–
–
–
–
–
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Slide 17
Arithmetic operators
Slide 18
Assignment Operators
Slide 19
Comparison Operators
Slide 20
Logical Operators
Slide 21
Identity Operators
Slide 22
Python Membership Operators
Slide 23
References
• Lutz, Mark (2013), Learning Python (5th Edition), Sebastopol, CA 95472:
O’Reilly Media, Inc. ISBN-10: 1449355730, ISBN-13: 978-1449355739
• W3schools
Slide 24
Download