0% found this document useful (0 votes)
10 views18 pages

Python Lesson 3 Variables, Types and Lists

Python Lesson

Uploaded by

Xuan Zheng Lim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
10 views18 pages

Python Lesson 3 Variables, Types and Lists

Python Lesson

Uploaded by

Xuan Zheng Lim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 18

Variables, Types and Lists

Dr. Saima Nisar


PhD (IT), MS (IT), BBA (Hons.)
saima.nisar@xmu.edu.my
A2-448
Learning Objectives
• In this chapter, you will learn:
o Basic syntax to display messages and variables with the print()
function
o The importance of indentation in defining code blocks in Python.
o Use different types of variables, including integers, floats, and strings
o Operations on strings and numbers, including arithmetic and
concatenation.
o Create, modify and iterate over lists using various methods
The print() Function

• Purpose: The print() function outputs text or variables to the

console. It is essential for displaying results and debugging

• Usage in Scripts: In Python scripts, print() is commonly used to

show the results of operations or to inform the user of the

program's progress.
The print() Function

• Syntax: print() can accept multiple arguments separated by

commas, which automatically inserts spaces between them.

• Example:

print("Hello World.")
print(“The result is”, 10+5)
print("Welcome to Python programming!")
Indentation and Code Structure

Python uses indentation for blocks, instead of curly braces. Both

tabs and spaces are supported, but the standard indentation

requires standard Python code to use four spaces

• Indentation: Python enforces indentation to represent code

blocks. It’s a fundamental aspect of Python’s design and makes

code more readable.


Indentation and Code Structure

• Block Definition: Indentation replaces the need for braces {} or

begin….end statement used in other languages.

• Consistency: Always use either spaces or tabs, but never mix

them. The convention is to use four spaces

• Functions and class definitions (def, class)


Indentation and Code Structure

• Common blocks that require indentation:


 Conditional statement (if, else, elif)
 Loops (for, while)
 Functions and class definitions (def, class)

temperature = 30
if temperature > 25: for i in range(0, 5):
print("It's a warm day.") print("Hello World.")

x = 10
if x > 5:
print("x is greater than 5") # This line is indented and part of the `if` block
Variables

• Python supports various data types such as integers, floating

point numbers, and strings. You can assign values to variables

without explicitly declaring the type.

• Example: x=5 y = 5.0


print(x) print(y)

x = 5 # x is an integer
x = "Hello" # Now x is a string
Variables
• Integers (int): Whole numbers, positive or negative

 Example: x = 5

• Floating-Point Numbers (float): Numbers with decimal points

 Example: y = 5.0

• Strings (str): A sequence of characters, enclosed in single or

double quotes

 Example: x = “Hello” OR x = ‘Hello’


String Concatenation
Python also supports string concatenation and simultaneous
variable assignment. Strings can be concatenated with the +
operator
# Numeric addition
•one
Concatenating
=1 Strings:
two = 2
three = one + two
print(three) # This will output: 3

# String concatenation
hello = “hello”
world = “world”
helloworld = hello + world # Concatenates with a space in between
print(helloworld) # This will output: hello world
Simultaneous Variable Assignment
Simultaneous Variable Assignment in Python refers to the ability
to assign values to multiple variables in a single line of code
using a tuple-like syntax. This allows for more concise and
readable code.Concatenating Strings:
x, y = 10, 20 # Assigns 10 to x and 20 to y at the same time

a, b = 3, 4
print(a,b)
Mixing operators
Mixing operators between numbers and strings is not supported

# This will not work! # This will work!


one = 1 one = "1"
two = 2 two = "2"
hello = "hello" hello = "hello"
print(one + two + hello) print(one + two + hello)

# This will work!


one = 1
two = 2
print("hello", one + two)
Lists
A list is a collection of items that can be of any data type, including
other lists (multidimensional lists).

Lists and Arrays: Lists in Python are similar to arrays in other


programming languages. You can think of an array as either a single list
(called a vector) or a grid of numbers (called a matrix).

Storing Items: Lists can hold any type of item, like numbers, words, or
even other lists. You can add as many items as you want.

Easy to Loop Through: You can easily go through each item in a list one
by one.
Example of Creating and Using a List
• Creating a List: You can create an empty list using square
bracket
mylist = [ ] # This creates an empty list

• Adding Items: You can add an item to the list using the
append() method
mylist.append(1) # This add the number
1 to the list
Example of Creating and Using a List
• Counting Starts at Zero: Lists start counting from zero. This
means the first item in the list is at position 0.
print(mylist[0]) # This prints the first item in the list, which
is 1

• Using Methods: You can use a period (.) followed by a


command to perform actions on the list.
mylist.append(2) # This add the number 2 to the list
Theory of Python’s Philosophy
• Readability Counts: Python was designed with readability in
mind. This is why code is clear and easy to follow, often
referred to as "executable pseudocode“.

• Simple Syntax: Python’s clean and straightforward syntax


reduces the barrier to entry for new programmers.

• Dynamic and High-Level: Python handles memory


management automatically, making it easier to focus on
problem-solving rather than system details.
Summary
• Variables store values and are dynamically typed.

• Print statements output values and text for feedback and


debugging.

• Indentation is required to define the structure and flow of the


code.

• Lists are flexible data structures used to store collections of


items.
Thank you

You might also like