0% found this document useful (0 votes)
18 views31 pages

TECH1200 Week 1 Workshop

This document outlines the fundamentals of programming with a focus on Python, covering topics such as variables, data types, and error handling. It includes learning outcomes, assessments, a weekly schedule, and guidelines for naming variables and using an integrated development environment. The document also provides examples and activities to reinforce understanding of programming concepts.

Uploaded by

Junaid Akram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views31 pages

TECH1200 Week 1 Workshop

This document outlines the fundamentals of programming with a focus on Python, covering topics such as variables, data types, and error handling. It includes learning outcomes, assessments, a weekly schedule, and guidelines for naming variables and using an integrated development environment. The document also provides examples and activities to reinforce understanding of programming concepts.

Uploaded by

Junaid Akram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

TECH1200

Fundamentals of
Programming
Lesson 1
Variables
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969

WARNING

This material has been reproduced and communicated to you by or on behalf of


Kaplan Business School pursuant to Part VB of the Copyright Act 1968 (the Act).

The material in this communication may be subject to copyright under the Act. Any
further reproduction or communication of this material by you may be the subject of
copyright protection under the Act.

Do not remove this notice.


Subject Learning Outcomes
1 Interpret simple program
specifications.
2 Produce a high-level model via the use of
pseudocode and flow charts.
3 Transfer a high-level model into a
software application via the use of a
programming language.
4 Use an integrated development
environment to develop, debug and
test a solution written in a
programming language.
5 Use a programming language to read and
write data to a persistent storage.
Subject Description
• This subject covers the fundamentals of software design
and introduces students to Python programming
language.

• This is complemented by instruction on the construction


of problem-solving techniques via the use of flow charts
and other common methods among IT professionals
such as pseudocode.

• This subject also addresses the principles and standard


procedures associated with the development and testing
of algorithms with an emphasis on the recommendation
of coding best practices.
Assessments
Assessment Weighting Learning Mode of Submission Details
outcomes submission week
Assessment 1: 20% LO1, LO2, MyKBS Week 5 Students will
Student Management LO3 develop a
System Development system using
control flow,
conditionals,
lists, and loops.
Assessment 2: 40% LO1, LO4 In-Class via Week 9 Students will
IT Service Desk System MyKBS develop a
Development system using
functions,
dictionaries, and
modules.
Assessment 3: 40% LO1, LO4, Stage 1: Stage 1: Students will
Employee Management LO5 In-Class Week 12 develop a
System Development system using
exception
Stage 2: Stage 2: handling and
MyKBS Week 13 classes.
Weekly Schedule
Topic

Week 1 Variables

Week 2 Control Flow

Week 3 Lists

Week 4 Loops

Week 5 Functions

Week 6 Study Success Week

Week 7 Strings

Week 8 Modules

Week 9 Dictionaries

Week 10 Files I/O

Week 11 Classes

Week 12 In-Class Assessment


Subject Resources
• External Online Resources
• Practice Questions on myKBS
• Applications:
– Microsoft Word
– Microsoft Visio
– PyCharm Community IDE
– ChatGPT
Variables
• Variables are containers located in
memory that are used to store values
processed by the programs.

• Values can be numbers, text, filename,


command or any other data.

• Unlike other programming languages,


variables are created in scripting
languages without specifying any data
types.

• This means the same variable can be


assigned any type of value, such as whole
number, decimal number, single character
and word, at different sections of the code
without any restrictions. https://medium.com/@RohitPatil18/variable-and-memory-references-in-python-29f976772ad3

• Python uses an interpreter to


automatically identify the data type of the
value stored in the variable.
Naming Variables (1/2)
The following rules must be followed for naming variables in
Python:

• The name of a variable must be meaningful and descriptive.

• Using Camel Case for variable names containing multiple


words e.g: hourlyRate.

• The name of a variable can only contain letters ( A-Z, a-z ),


numbers ( 0-9 ) and an underscore ( _ ) character.

• The name of a variable can only start with a letter or an


underscore ( _ ) character.
Naming Variables (2/2)
The following rules must be followed for naming variables in
Python:

• The name of a variable cannot start with a number ( 0-9 ).

• The name of a variable is case sensitive.

• The name of the variable cannot contain any whitespace.

• The name of the variable cannot contain any other special


characters except the underscore ! ? * # < > [ ] % @ % $ { } - /
:;&()\+=,^~|.

• Python keywords can’t be used as a variable name.


Python keywords
These Python keywords cannot be used as variable
names:
Creating variables
• The following syntax is used to create variables with values in
Python:
<VARIABLE NAME> = <VALUE>

Examples of defined variables:


– name = "Anna"
– age = 25
– grade = 'B'
– rate = 5.79

Note: There must be whitespace around the “=“ sign.


• city="Berlin" --> invalid coding standard
• country = "Germany" --> valid coding standard
Displaying variables (1/2)
• The print() statement is used to display variables:
variable = "Python is Awesome!"
print(variable)

• "Python is Awesome!" in this case is called a String.

• Strings must be surrounded by either:


– Single quotation marks ‘’
– Double quotation marks “”

• It does not matter which is used, but ensure consistent


usage to follow coding standards.
Displaying variables (2/2)
• The + character can be used to combine
variables with text to display them:

country = "Japan"
print("Tokyo is the capital city of " + country)

Note: If the value of the variable is not a string it


must be converted to a string first before it can be
displayed with text.
Activity
What is the output of this code…?
variable = 100
variable = "This is the value"
print(variable)
Answer:
This is the value

Reason:
• The stored value in variable can be
changed after it has been set due to line-by-
line execution.
Data Types (1/3)
The following data types are used in Python for storing
values in variables:
• Integers: numeric values without decimal points ( int )
• Floats: numeric values with decimal points ( float )
• Strings: text values composed as sequence of
characters ( str )
• List: ordered collection of multiple mutable values (
list )
• Tuple: ordered collection of multiple immutable values
( tuple )
• Boolean: True and False values ( bool )
Data Types (2/3)
Below are examples of data types being used
in variables:

• Integer: num = 100

• Float: price = 7.95

• String: num = "Japan"

• List: fruits = ["Apple", "Banana", "Orange"]

• Tuple: vegetables = ["Potato", "Carrots", "Onions"]

• Boolean: person = True


Data Types (3/3)
The type() function can be used to identify
the data type of a variable:

name = "Michael"
age = 24

print(type(name))
print(type(age))
Activity
• For each of the variables, what type of
variable is it? a = 0
b = 42.1
c = -912
d = "678"
e = 13513517
f = 0.0
g = -13.012

Answer: variable a: <class 'int'>


variable b: <class 'float'>
variable c: <class 'int'>
variable d: <class 'str'>
variable e: <class 'int'>
variable f: <class 'float'>
variable g: <class 'float'>
Data Type Conversions
The following functions are used to convert between
data types of variables in Python:
• Converting value of variable to an integer: int()
• Converting value of variable to a float: float()
• Converting value of variable to a string: str()
• Converting value of variable to a Boolean: bool()
• Converting value of variable to a list: list()
• Converting value of variable to a tuple: tuple()
Comments
• Comments start with a #
1. Comments can be at the start of a line
# Our first program to print "Hello World!"
variable_name = "World"
print("Hello " + variable_name + "!")

2. Comments can be at the end of a line


variable_name = "World" # Change "variable_name" to your name
print("Hello " + variable_name + "!")

3. Comments can ignore a whole line of code


#variable_name = "World"
#print("Hello " + variable_name + "!")
print("The above two lines are ignored by Python.")
Errors
• We will make errors… But, how does the
program communicate this to us?
– PyCharm or any other Integrated
Development Environment (IDE) will:
• Display the error type
• Information where the error has occurred
• Two common error types in Python:
– SyntaxError
– NameError
SyntaxError
• SyntaxError is produced when we did not
adhere to the Python syntax
– Let’s attempt this code…
some_variable = "VALUE'

– PyCharm console displays the following error:


some_variable = "VALUE'
^
SyntaxError: unterminated string literal (detected at line 1)
– Solution: in this case, some_variable variable,
must have consistent String usage. Both double
quotes or both single quotes, not one of each.
some_variable = "VALUE"
NameError
• NameError is produced when we did not
define a variable
– Let’s attempt this code…
print(some_variable)

– PyCharm console displays the following error:


print(some_variable)
NameError: name 'some_variable' is not defined

– Solution: some_variable variable must be


defined and a value must be assigned to the
variable, before the print() function statement
some_variable = "VALUE"
print(some_variable)
Activity
• What error(s) do you get with this code:
numerator = 777
denominator = 0
result = numerator / denominator
print(result)

Answer:
• ZeroDivisionError – occurs when a
number (denominator) is divided by a 0 or
0.0 result = numerator / denominator
ZeroDivisionError: division by zero
Calculations
• Python can compute math questions
– Addition with a plus sign +
print(69 + 5)

– Subtraction with a minus sign –


print(6 - 87)

– Multiplication with an asterisk *


print(6.2 * 100)

– Division with a slash /


print(3 / 37)
Exponents
• Python can compute exponentiation with a
double asterisk **
# 2 to the 12th power, 2^12, or 4096
# or... 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
print(2 ** 12)

# 6 squared, 6^2, or 36
print(6 ** 2)

# 2 to the half power, 2^0.5, or 1.41


print(2 ** 0.5)
Modulo
• Python can compute modulo operation
with a percent sign %, which returns the
remainder of the division operation
# Prints 3
# 23 / 5 is 4, with a remainder of 3
print(23 % 5)

# Prints 1
# Important Note for "Modulo % 2"
# Returns "0 for even numbers" and "1 for odd numbers"
print(21 % 2)

# Prints 4
# 4 / 8 is 0, with a remainder of 4
print(4 % 8)
Concatenation
• The plus sign + can add two str types or Strings (and
not just numeric types)
# prints "HelloWorld!"
a = "Hello"
b = "World!"
c = a + b
print(c)

• However, you cannot concatenate a str type with an int


type
# prints TypeError: can only concatenate str (not "int") to str
a = "Hello"
b = 5
print(a + b)
Plus Equals
• A shortcut to update a variable value can
be done by using the plus equals sign +=
# prints 105
distance = 100
distance += 5 # same as distance = distance + 5
print(distance)

# prints add words to the String added


message = "add words to the String"
message += " added" # same as message = message + " added"
print(message)
Multi Line Strings
• Strings can be assigned multiline values by
using three double or single quotes at the
start and end of variable assignment
variable = """
I can
I can
write
write
here Output
here
or here
or here
:O
:O
"""
print(variable)

• Notice the empty line in the first and last line

You might also like