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

Gr7 - Introduction To Python

This document serves as an introduction to Python, highlighting its user-friendly nature and real-world applications. It covers fundamental concepts such as data types, operators, variables, and the importance of casting in Python programming. Additionally, it emphasizes the use of Python's IDLE for writing and testing code, making it accessible for both beginners and experienced developers.

Uploaded by

aploosaziz12
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)
10 views6 pages

Gr7 - Introduction To Python

This document serves as an introduction to Python, highlighting its user-friendly nature and real-world applications. It covers fundamental concepts such as data types, operators, variables, and the importance of casting in Python programming. Additionally, it emphasizes the use of Python's IDLE for writing and testing code, making it accessible for both beginners and experienced developers.

Uploaded by

aploosaziz12
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

INTRODUCTION TO

PYTHON

In this chapter, students should be able to:

 Comprehend the importance of Python as a user-friendly language.


 Understand the real-world use of Python.
 Implement various algorithms in Python to solve specific tasks.
 Analyze and debug Python code.

Introduction
Python is a widely used, general-purpose, high-level programming
language known for its simplicity and readability. It was created by
Guido van Rossum and first released in 1991. Python is designed with
an emphasis on code readability, making it an excellent choice for
both beginners and experienced developers.

PYTHON – A Full-Spectrum Language


Python is often referred to as a full-spectrum programming language, meaning it is both beginner-friendly and
powerful enough for professional software development.
Some programming languages are designed to be extremely easy for beginners. A great example is Scratch, a
visual programming language where users drag and drop blocks representing programming concepts like
variables, loops, and conditionals. While Scratch simplifies learning, it is not suitable for developing advanced,
professional applications.
On the other end of the spectrum, powerful languages like C and C++ exist. These languages allow developers
to build complex and highly efficient applications, including operating systems, game engines, and embedded
systems. However, they come with a steep learning curve, requiring deep knowledge of memory management,
pointers, and object-oriented programming. Python strikes a perfect balance between simplicity and power. It
is easy to read and write, making it an excellent choice for beginners.

The Academy | Computer Handout | Introduction to Python | Grade 7 Page | 1


Features of Python

Applications of Python

The Academy | Computer Handout | Introduction to Python | Grade 7 Page | 2


Data Types in Python
In Python, the built-in data types that you can consider basic are the following:
1. Integer Numbers
In Python, integers (int) are whole numbers without decimals. They can be positive, negative, or zero (e.g.,
-5, 0, 42).
2. Floating-Point Numbers
Floats (float) represent real numbers with decimals or scientific notation. They are used for precise
calculations requiring fractions (e.g., 3.14, 0.5, 2e5).
3. Strings and Characters
Strings (str) are sequences of characters enclosed in quotes. They store text, symbols, or numbers as text
(e.g. “Hello”, “Python123”).
4. Booleans
Booleans (bool) hold either True or False. Used in conditions, comparisons, and logic operations.

Operators in Python
1. Arithmetic
Used for mathematical operations like addition, subtraction, multiplication, and division.
Sign Example
+ (Addition) 5+3
- (Subtraction) 10 - 4
* (Multiplication) 4*2
/ (Division) 8/2
** (Exponentiation) 25

2. Comparison
Used to compare values and return a Boolean result (True or False).
Sign Example
== (Equal to) 5 == 5 # True
!= (Not equal to) 4 != 5 # True
> (Greater than) 10 > 3 # True
< (Less than) 2 < 5 # True
>= (Greater than or equal) 6 >= 6 # True
<= (Less than or equal) 4 <= 7 # True

3. Logical
Used to combine conditional statements and return Boolean results.
Sign Example
and (Logical AND) (5 > 3 and 10 > 5) # True
or (Logical OR) (5 > 10 or 3 < 8) # True
not (Logical NOT) not False # True

The Academy | Computer Handout | Introduction to Python | Grade 7 Page | 3


Variables
Variables are nothing but reserved memory locations to store values. This means that when you create a
variable, you reserve some space in memory. Based on the data type of a variable, the interpreter allocates
memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types
to variables, you can store integers, decimals or characters in these variables.

Rules for Python Variables


 A variable name must start with a letter (A–Z, a–z) or an underscore (_)
 A variable name cannot start with a number. Python will throw an error if you try to do this. Why?
Python thinks you're trying to do a calculation or something unexpected if a name starts with a number.
 A variable name can only contain alpha-numeric characters and underscores (A–Z, a–z, 0-9, and _ ). No
spaces, hyphens (-), or special characters like @, $, %, etc.
 Variable names are case-sensitive. This means Python treats uppercase and lowercase letters as
different. (age, Age and AGE are three different variables)

Assigning Values to Variables


Python variables do not need an explicit declaration to reserve memory space. The declaration happens
automatically when you assign a value to a variable.
The equal sign (=) is used to assign values to variables. The operand to the left of the = operator is the name
of the variable, and the operand to the right of the = operator is the value stored in the variable.

For example:
a= 100 # an integer assignment
𝑏 = 1000.0 # 𝐴 𝑓𝑙𝑜𝑎𝑡𝑖𝑛𝑔 𝑝𝑜𝑖𝑛𝑡
𝑐 = "𝐽𝑜ℎ𝑛" # 𝐴 𝑠𝑡𝑟𝑖𝑛𝑔
𝑝𝑟𝑖𝑛𝑡 (𝑎)
𝑝𝑟𝑖𝑛𝑡 (𝑏)
𝑝𝑟𝑖𝑛𝑡 (𝑐)

This produces the following result:

100
1000.0
𝐽𝑜ℎ𝑛

Writing a program in Python


Once you have planned a solution using a flowchart, you can then create a solution in Python. This makes it easier
to solve, as you have already broken down the problem and worked out the order; you need only get
the syntax correct.

Python is normally installed on a computer with IDLE. It is an acronym for Integrated Development and Learning
Environment. IDLE provides lots of functions that are useful when writing Python code. Using IDLE to program
in Python makes it much easier to write, test and run your code. Click on the IDLE (Python) icon on your computer
to launch the program.

The Academy | Computer Handout | Introduction to Python | Grade 7 Page | 4


When you launch Python IDLE, the prompt >>>, on the last line shows that you are in the Python shell. This is
the interactive mode, which allows individual Python commands to be typed directly and carried out.

In the example above, you can see a print statement. The print statement is a Python command that outputs
information to the screen. This print statement will output the words ‘Hello world’ onto the screen. When using
Python code, you need to ensure that you type commands using lower-case letters. Python can tell the difference
between upper-case (capital) letters and lower-case (small) letters (it is case sensitive). For example, if you
enter Print ("Hello world"), an error should occur because ‘Print’ (with a capital ‘P’) is not the same as ‘print’
(with a small ‘p’). You should always type Python commands in lowercase.
The next line (7+6)*2 will output the result of the calculation. Notice that you do not need to use the print
commands when performing calculations in the shell. Python can use arithmetic symbols such as + for adding, /
for dividing and * for multiplying. To close Python, type exit().

Casting in Python
The process of ensuring data is set to the correct data type is known as casting. It is very important when
programming in Python that you always consider what data type the variable is and ensure it is set to the right
type using casting. This is particularly important when using variables to perform calculations. If you do not
inform Python of the data type of variables, it will not work correctly.
Consider the following example:

The program above should multiply the number entered by 2 and answer. If 5 was entered, it should say 10, but
this is not what happens:

The Academy | Computer Handout | Introduction to Python | Grade 7 Page | 5


Code What does it do?
The input statement is cast to an integer as it is wrapped in the
num1 = int(input("Enter a number: ")) code int( ). This means that Python stores the value entered in
the variable num1 as an integer.
This is exactly the same as the previous example, except the
input statement is wrapped in the code float( ). This means that
num1 = float(input("Enter a number: "))
Python stores the value entered in the variable num1 as a
decimal number.

The input function captures user input as a string data type. This means that if you want to perform a calculation
using the input, you will need to cast it to either an integer (int) or a decimal (float). Below shows an example of
how you cast an input to a different data type:
If the original code is now amended to cast the input to an integer, it will look like this:

When the program is run, it now gives the correct answer:

Sample programs of Python


 Calculating the area of a rectangle

 Calculating the sum of two numbers

The Academy | Computer Handout | Introduction to Python | Grade 7 Page | 6

You might also like