Introduction To Python Computer Science + AI
Introduction To Python Computer Science + AI
Learning Outcomes
Learners will be able to:
Communication
Comic Play
Let’s begin by printing ‘Hello,
How do I start coding World!’, it’s the first step in Python.
in Python, Guru?
Thanks, didi!
Coding is fun!
83
2. The sprite should change the colour effect before broadcasting “Jump”.
Programming is giving instructions to a computer to perform specific tasks. Imagine you are
writing a list of steps for your friend to follow—like how to bake a cake or solve a puzzle.
Similarly, computers need instructions, called programs, to know what to do.
Computers follow instructions exactly as they are written. A computer cannot think for itself,
Syntax refers to the rules governing the structure and format of a specific programming
so it needs clear and precise directions. When a programmer writes a program, they tell the
language.
computer how to complete a task step by step.
It dictates how statements, commands, and instructions are written and organised in a
way that the computer can understand and execute them.
Input
Computer
Unlike some other languages (C++, Java, etc.) that uses Output
(Instruction + Data)
complicated syntax, Python uses simple and clear
print(“Hello,World”)
commands that are easy to understand. This makes it a
fantastic starting point for anyone new to programming.
Direct Perception
PYTHON
Tech Fact
Python is a programming language that is easy to learn and read, making it an
FACTS
ideal choice for beginners. The simplicity of Python makes it perfect for those
Python
just starting their is one of the most
programming popular
journey. programming
Python was languages
createdininthe world.
the lateIt's widelyby
1980s
used by developers and is considered a great language for learning programming.
Dutch programmer Guido van Rossum.
The first version, Python
Python's role0.9.0, wasfields
in various released in February 1991. Over the years,
Python has undergone
A computerseveral
program major updates,
is a sequence or set with Pythonin3abeing
of instructions a significant
programming languagemilestone.
Python is not just
for a a language
computer for making
to execute. Python issmall
not justprograms;
a language foritmaking
is used in programs;
small lots of important
it is and
exciting areas:used in lots of important and exciting areas: Application
Web Software Database Game
Development Development Access Development
Tech Adventure
</>
84 • Web Development: Think of websites you visit daily. Many of them are built using
Python. Python is like paint and brushes for artists creating the digital world.
• Data Science: Imagine being a detective, solving mysteries hidden in numbers
and data. Python helps experts analyse information and find important patterns.
PW_Computer_G6_Ch08.indd 84
Example: Prediction of malfunctions in a car based on previous car data, and image 29-11-2024 16:14:03
Cyber Tip
Python stands in second place in the list of most used languages. Testimony
Understanding Syntax
Syntax refers to the set of rules that govern how code is written and structured. It dictates
how statements, commands, and instructions are organised so that the computer can
interpret and execute them correctly.
Unlike programming languages such as C++ or Java, which have more complex syntax,
Python is known for its simple, readable commands. This ease of understanding makes
Python a fantastic starting point for beginners.
Binary Bytes
Analogy
Syntax: Refers to the rules or structure of how code is written (like the grammar
of programming languages).
Semantics: Refers to the meaning or function of the code when it runs (what the code
actually does).
85
Tech Insight
Testimony
Python was named after the British comedy series “Monty Python’s Flying Circus”,
not the snake! The language’s creator, Guido van Rossum, was a fan of the series.
INSTALLING PYTHON
Follow the below steps to download and install the Python software:
1 Visit the URL [Link]/downloads using browser.
Click on Download
Python 3.13.0
button.
2
Open the
download file.
3
Click on Install
Now and follow
the steps. 5
Tech Adventure
86
To start coding in Python, you need to install Python on your computer. Once installed, you can
use a Python Integrated Development Environment (IDE), like IDLE, which comes with Python.
2
3
87
Script Mode is the best way to create longer programs, and you can run the script multiple
times without having to retype your code.
Syntax: print(“value”)
Input And Output
The print() Command
The print() statement displays a value or information in the output.
Code Quest
Write a Python program that asks the user for their favourite colour and Applying
then prints a message saying, “Your favourite colour is [colour]!”
Tech Adventure
88
Note: If we change the value of a variable and reuse it, the old value is switched to the
new value.
89
Data Types
Data types define what type of data a variable, a named memory location, can hold.
Data Type Description Example
Boolean (bool) Stores two values: True and False True, False
Comments
Comments are an essential part of writing clean and understandable code. They are notes or
explanations written by the programmer that the Python interpreter ignores.
Multi-Line Comment
For multi-line comments, Python doesn’t have a specific syntax. However, multiple single-line
comments or a string of triple quotes (‘’’ or “””) can be used for documentation purposes.
90
Operators are symbols that perform operations on variables and values. Python includes
various types of operators, each serving a specific purpose.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
- Subtraction x - y 3
* Multiplication x * y 18
/ Division x / y 2.0
// Floor Division x // y 2
% Modulus (Remainder) x % y 0
** Exponentiation x ** y 216
Example:
a = 12 Addition: 17
b = 5 Subtraction: 7
print (“Addition:”, a + b) Multiplication: 60
print (“Subtraction:”, a - b) Division: 2.4
print (“Multiplication:”, a * b)
Modulus: 2
print (“Division:”, a / b)
Exponentiation: 248832
print (“Modulus:”, a % b)
print (“Exponentiation:”, a ** b) Floor Division: 2
print (“Floor Division:”, a // b)
91
= Assignment x = 5 5
+= Addition Assignment x += 3 8
-= Subtraction Assignment x -= 2 3
*= Multiplication Assignment x *= 4 20
%= Modulus Assignment x %= 3 2
Example:
a = 10 15
b = 5 10
a += b 50
print(a) 10.0
a -= b
0.0
print(a)
0.0
a *= b
0.0
print(a)
a /= b
print(a)
a %= b
print(a)
a **= b
print(a)
a //= b
print(a)
Tech Adventure
92
Example:
a = True False
b = False True
print(a and b) False
print(a or b)
print(not a)
Relational Operators
Relational operators are used to compare values.
Operator Description Example Output
== Equal to 5 == 3 False
Example:
a = 10 False
b = 5 True
print(a == b) True
print(a != b) False
print(a > b)
True
print(a < b)
False
print(a >= b)
print(a <= b)
93
Code Quest
Given two variables, a and b, perform the following operations in Python and Applying
answer the questions
a = 8 b = 3
1 What is the result of a + b?
2 What is the result of a - b?
3 What is the result of a * b?
4 Is a greater than b?
5 Compute (a + b) / b and check if the result is greater than 3.
Examples of Precedence
Unary and Binary Operators:
result = -2 + 3 * 4 10
print(result)
Tech Adventure
Parentheses:
result = (2 + 3) * 4 20
print(result)
94
Comment: Notes in code that explain what it does, are ignored by the computer.
Function: A block of code designed to perform a specific task.
Input: Data that a user provides to a program.
Interactive Mode: A way to test code immediately.
Library: A collection of pre-written code for common tasks.
Operator: A symbol that performs operations on values (e.g., +, -).
Output: Information that a program displays or returns.
Programming: Giving a computer instructions to perform tasks.
Python: A simple and popular programming language.
Readability: How easy it is to understand code.
Script Mode: Writing and saving code in a file.
Semantics: The meaning of the code when it runs.
TECH QUEST
95
Coding Questions
1. Write a Python program that prints the message “Welcome to Python Application
Programming!”
2. Create a Python program that asks the user for their age and then prints “You are [age]
years old!”
3. Write a Python code snippet to perform the following calculations and print the results:
Addition of 10 and 5, Subtraction of 10 from 15, Multiplication of 4 and 5.
4. Write a Python program to store the values 20 and 15 in two variables, then swap their
values and print the results.
Application-Based Questions
1.
If you want to store the score of a game in a Python program, what Application
type of variable would you use and why?
2. You have a list of names in a variable and want to print them one by one. Write a
Python snippet to achieve this.
3. Imagine you are building a simple calculator in Python. Which operators would you use
Tech Adventure
96
START
1 -7-3-3-1(-1) 12 4-(-2)-3
10-(-9)
-1 19 1 -1 -14
2 4 -1 -4 6
Critical Thinking
Dive in Activity: Find the error!
Underline the code below where you find the error and write the correct answer in blanks.
1.
print(“Hello World)
2.
name, number = “Rahul”, 10
print(word, number)
3.
a = 12
b = 5
print(“Division:”, a // b)
4.
name = input(‘Enter your name: ’)
print(“Hello,” + name!)
5.
a = True
b = False
print(a or and b)
97
Experiential Learning
Dive in Activity: Find the output!
1.
colour = input(“Enter your favourite colour: “)
print(“Your favourite colour is “ + colour + “!”)
2.
num1 = int(input(“Enter the first number: “))
num2 = int(input(“Enter the second number: “))
print(“The sum is:”, num1 + num2)
3.
length = float(input(“Enter the length of the rectangle: “))
width = float(input(“Enter the width of the rectangle: “))
area = length * width
print(“The area of the rectangle is:”, area)
Tech Adventure
98
5.
celsius = float(input(“Enter temperature in Celsius: “))
Fahrenheit = (celsius * 9/5) + 32
print(“Temperature in Fahrenheit is:”, Fahrenheit)
99