COMP002 Lesson 3 Basic Prog PPT3
COMP002 Lesson 3 Basic Prog PPT3
in
ACCORDING TO:
List
A list can contain a series of values. List variables are
declared by using brackets [ ] following the variable name.
Tuple
Tuples are a group of values like a list and are manipulated
in similar ways. But, tuples are fixed in size once they are
assigned. In Python the fixed size is considered immutable
as compared to a list that is dynamic and mutable. Tuples
are defined by parenthesis ().
PYTHON DATA TYPES
Dictionary
Dictionaries in Python are lists of Key: Value pairs. This is
a very powerful datatype to hold a lot of related
information that can be associated with keys. The main
operation of a dictionary is to extract a value based on
the key name. Unlike lists, where index numbers are
used, dictionaries allow the use of a key to access its
members. Dictionaries can also be used to sort, iterate,
and compare data.
helloworld.py
print("Hello, World!")
Python Syntax
Example
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
You have to use the same number of spaces in the same block of code, otherwise,
Python will give you an error:
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Python Variables
In Python, variables are created when you assign a value to it:
Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
Example
Variables in Python:
x = 5
y = "Hello, World!“
print(x)
print(y)
Python Variables(continuation)
Variables do not need to be declared with any particular type, and can
even change type after they have been set.
Example
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Casting
If you want to specify the data type of a variable, this can be done with casting.
Example
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Comments
Comments in Python:
#This is a comment.
print("Hello, World!")
Python - Variable Names
Variable Names
•A variable can have a short name (like x and y) or a more descriptive name
(age, carname, total_volume). 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)
•A variable name cannot be any of the Python keywords.
ExampleGe
t your own Python Serve
Legal variable names:
myvar = "John"
my_var = "John" Example
_my_var = "John" Illegal variable names:
myVar = "John"
MYVAR = "John" 2myvar = "John"
myvar2 = "John“ my-var = "John"
my var = "John"
print(myvar)
print(my_var)
print(_my_var)
print(myVar)
print(MYVAR)
print(myvar2) Remember that variable names are case-sensitive
Multi Words Variable Names
Variable names with more than one word can be difficult to read.
There are several techniques you can use to make them more readable:
Camel Case
Each word, except the first, starts with a capital letter:
myVariableName = "John"
Pascal Case
Each word starts with a capital letter:
MyVariableName = "John"
Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"
Python Variables - Assign Multiple
Values
Python allows you to assign values to multiple variables in one line:
ExampleGet your own Python Serve
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Note: Make sure the number of variables matches the number of values, or else you will
get an error.
One Value to Multiple Variables
And you can assign the same value to multiple variables in one
line:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to extract the values into
variables. This is called unpacking.
Example
Unpack a list:
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Python - Unpack Tuples
Unpacking a Tuple
When we create a tuple, we normally assign values to it. This is called
"packing" a tuple:
ExampleGet your own Python Serve
Packing a tuple:
fruits = ("apple", "banana", "cherry")
in Python, we are also allowed to extract the values back into
variables. This is called "unpacking":
Example
Unpacking a tuple:
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow) Note:The number of variables must match the number
of values in the tuple, if not, you must use an asterisk
print(red) to collect the remaining values as a list.
Using Asterisk*
If the number of variables is less than the number of values, you can add
an * to the variable name and the values will be assigned to the variable
as a list:
Example
Assign the rest of the values as a list called "red":
fruits =
("apple", "banana", "cherry", "strawberry", "raspberry")
print(green)
print(yellow)
print(red)
If the asterisk is added to another variable name than the last, Python
will assign values to the variable until the number of values left
matches the number of variables left.
Example
Add a list of values the "tropic" variable:
fruits =
("apple", "mango", "papaya", "pineapple", "cherry")
print(green)
print(tropic)
print(red)
»
SEATWORK # 3- Python Syntax
1. # Fix the indentation errors in the following code:
def greet(name):
print(“Hello”, name + “!”)
print(“Welcome to Python!”)
greet(“Alice”)