Introduction to Python
Python Introduction
It 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
Python Introduction
Python can be used:
in a server to create web applications.
alongside software to create workflows.
to connect to database systems. It can also read and modify files.
to handle big data and perform complex mathematics.
for rapid prototyping or for production-ready software development.
Python Indentation
INDENTATION refers to the spaces at the beginning of a code line.
In other programming languages, INDENTATION in code is used for
readability only, while in Python it is very important
Python uses INDENTATION to indicate a block of code.
if sum < 20
print(“sum is less than 20”)
Python Comments
Python has commenting capability for the purpose o in-code
documentation.
Comments start with a # and Python will render the rest of the line as a
comment.
#this is a comment.
print(“Hello world!”)
Python Variables
VARIABLES are containers for storing data values.
Unlike other programming languages, Python has no command for declaring a VARIABLE.
VARIABLE is created the moment your first assign to it.
VARIABLE can have a short name(like a, b, x or y) or a more descriptive name (age,
Fname, total, product, volume)
Rules for Python variables:
A variable name 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, a-z, 0-9 and _)
Variable names are case-sensitive (age, Age and AGE are three different variables.
x=5
Y = “John”
print(x)
print(y)
Python Data Types
In programming, data type is an important concept
Variable can store data of different types, and different things.
Python has the following data types built-in by default. In these categories:
Text type: str
Numeric type: int, float, complex
Sequence type: list, tuple, range
Mapping type: dict
Set type: set, frozenset
Boolean type: bool
Binary type: bytes, bytearray, memoryview
Python Data Types
Example Data Type
x = “Hello World” str
x = 20 int
x = 2.5 float
x = 1j complex
x = [“apple”, “banana”, “cherry”] list
x = (“apple”, “banana”, “cherry”) tuple
x = range(6) range
x = {“name” : “John”, “age” : 36} dict
x = {“apple”, “banana”, “cherry”} set
Python Data Types
Example Data Type
x = frozenset([“apple”, “banana”, “cherry”,]) Frozenset
x = True Bool
x = b”Hello” Bytes
x = bytearray(5) Bytearray
x = memoryview(bytes(5)) memoryview
Python Numbers
There are three numeric types in Python:
int
float
complex
Python Strings
String literals in python are surrounded by either single quotation or double
quotation marks
‘hello’ is the same as “hello”.
You can display a string literal with the print() function:
Python Assignment Operators
Python Collections (Array)
Python List
Loop Through a List
List Length
Add Items to List
Removing an Item in a List
Removing an Item in a List
Changing Values in a List
Join Two Lists
Changing Tuple Values
Join Two Sets
IF - Else
Python Loops
Function in Python
Function in Python
Return value in Calling Function in
Python
- To let a function return a value, use
the return statement:
Python Recursion
RECURSION is the process of
defining something in terms of itself.
- In Python, we know that a
function can call other functions.
- It is even possible for the function
to call itself. These type of
construct are termed as recursive
function.
Python Recursion
RECURSION is the process of
defining something in terms of itself.
- In Python, we know that a
function can call other functions.
- It is even possible for the function
to call itself. These type of
construct are termed as recursive
function.
Activity 1:
Activity 2: