0% found this document useful (0 votes)
2 views11 pages

Python Notes

The document provides comprehensive lecture notes on basic Python programming, covering topics such as variables, data types, operators, lists, control structures, and functions. It explains how to work with strings, numbers, and Booleans, as well as how to implement conditional and loop structures. Additionally, it discusses algorithms, program structure, machine code, and common programming errors like syntax and logical errors.

Uploaded by

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

Python Notes

The document provides comprehensive lecture notes on basic Python programming, covering topics such as variables, data types, operators, lists, control structures, and functions. It explains how to work with strings, numbers, and Booleans, as well as how to implement conditional and loop structures. Additionally, it discusses algorithms, program structure, machine code, and common programming errors like syntax and logical errors.

Uploaded by

Kelvin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Pa ge |1

Basic Python Lecture Notes

 Python is a popular programming language. It was created by Guido van Rossum and
released in 1991.
 Python is a text-based programming language, and its syntax is very simple.
 Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
 Python can be treated in a procedural way, an object-oriented way or a functional
way.
 In Python, indentation is important, and it is used to indicate a block of code.

Variables
 Variables are containers for storing data values.
 In Python variables, you can store a single value at one time.
 Variables that are created outside of a function are known as global variables.
 If you want to specify the data type of a variable, this can be done with casting.
 int() - constructs an integer number from an integer literal, a float literal (by removing
all decimals), or a string literal (providing the string represents a whole number)
 float() - constructs a float number from an integer literal, a float literal or a string
literal (providing the string represents a float or an integer)
 str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals

Data Types
 In programming, data type is an important concept.
 A computer stores all data using a digital code. For example, text characters are stored
using ASCII code. ASCII characters are stored as string data type.
 Python has the following data types built-in by default, in these categories:

 You can get the data type of any object by using the type() function:
Pa ge |2

Python String Data


 String data can include any keyword characters. The characters of the string are
shown inside quote marks. You can use double or single quote marks.

Eg. “hello”

“I am 99 years old”

“99”

 In Python, user input is stored as a string.


 To get the length of a string, use the len() function.
Eg.
a = "Hello, World!"
print(len(a))
 To concatenate, or combine, two strings you can use the + operator.
Eg.
a = "Hello"
b = "World"
c=a+b
print(c)

Python Numbers
 There are three numeric types in Python: int, float and complex.
 Int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
o x=1
y = 35656222554887711
z = -3255522
 Float, or "floating point number" is a number, positive or negative, containing one or
more decimals.
o x = 1.10
y = 1.0
z = -35.59
 Complex numbers are written with a "j" as the imaginary part:
Pa ge |3

o x = 3+5j
y = 5j
z = -5j

Python Booleans
 Booleans represent one of two values: True or False.
 In programming you often need to know if an expression is True or False.
 You can evaluate any expression in Python, and get one of two
answers, True or False.
 When you compare two values, the expression is evaluated and Python returns the
Boolean answer:

Python Operators
 Operators are used to perform operations on variables and values.
 Study more about operators from this link:
Python Operators ([Link])

List
 A list is an example of a data structure. A data structure is a type of variable that can
hold many values. In a list data structure, the different values are called elements.
 In Python, list is a collection which is ordered and changeable. It also allows duplicate
members.
 In Python, the first item in the list has index value of 0.
 If you give the large index number to print out, the program will crash and it shows
the error message, “out of bounds error”.

o One way to reduce the error is to give the helpful information.

 A program which does not crash is called the robust program.


 To make user your program is robust, you can add a command that blocks bad input.
A check which blocks bad input is called a validation check.
 One way to do validation is to use an if… else structure.

List Methods
Pa ge |4

 Append means add to the end.


colourlist=[“red”, “green”, “blue”]
[Link](“orange”)

or ask the user input to append


color = input(“Enter new color”)
[Link](color)

 Add the elements with specific index number


colourlist=[“red”, “green”, “blue”]
[Link](1, “white”)

 Print the whole list


print(colourlist)

 Print the individual list item with index number


print(colourlist[1])
(or)
i = int(input(“Enter the index number to print out”))
print(colourlist[i])

 Edit the list elements


colourlist[1] = “gold”
(or)
i = int(input(“Enter the index number to edit”))
colourlist[i] = “gold”

 Delete the list elements with the index number


del colourlist[2]
(or)
i = int(input(“Enter the index number to delete”))
del colourlist[i]

 Remove the list elements with the name of elements


name = input(“Enter the list element to remove”)
[Link](name)

 Sort the list


[Link]()
Pa ge |5

How to traverse a list?


 Traversing a data structure means looking at every value in the data structure.
Eg.
student_list = ["David", "Jack", "Thormas", "Mike", "Leo", "Elisa", "Grace"]

Program Interface
A program interface is the part of a program that handles input and output.
 Input: The interface gets user inputs. The user can control the program.
 Output: The interface displays program outputs. The user can see results and content.

If --- else structure (Conditional Structure)


 It is called a selection command.
 Python if---else structure is made out of a header and a body.
 The header has the commands that control the if structure. The body has the
commands that are controlled by the header.
Pa ge |6

 The commands in the body are indented to show that they belong inside the if
structure.

Loop Structure
 A loop also has the header/body structure.

 If one structure is inside the another, this is called a nested structure.


 A nested structure is shown using double indentation.

Two types of looping in Python:


In most programming languages, every loop has an exit condition. The exit condition is
how you stop the loop.
1. For loop
 A for loop is controlled by a counter. It is counter-controlled loop.
2. While loop
 A while loop is controlled by a logical test. It is a condition-controlled loop.

Procedure/Function
 A procedure is a type of module that you can make yourself.
 A module is a ready-made block of code, with a name, that you can use in your
program.

Example Procedure
 def is the Python keyword to define a procedure.
 def stands for “define procedure.”
Pa ge |7

 Using procedures and other modules in your programming is called modular


programming.
 Python have some modules that have been written for you. They are called built-in
functions. Eg. input(), int(), print()

Search a List
 When you do a search, the item you look for is called the search term.
 The relational operator ‘in’ compares a single value and a list.

 There are other ways to search the list: Linear search and Binary search.
Pa ge |8

 The ‘return’ command is a Python key word. When the computer sees the return
command, it will stop the procedure and return to the main program.

 The binary search is much faster than the linear search. But it has one drawback. You
can only use the binary search on a sorted list.
 The middle position in a list is called the midpoint.
 The value stored in this position is the midpoint value.
Pa ge |9

 This command shows all the values up to the midpoint:


print(atoms[:midpoint])
 This command shows all the values from the midpoint to the end of the list:
print(atoms[midpoint:])
 In the binary search procedure: binsearch(atoms), atoms is the parameter to send the
copy of the atoms list. Sending the copy is called passing a parameter.

Algorithm
 An algorithm is a plan to solve a problem.
 All the actions of a task are taken in the right order.
 Drawing an algorithm:
o Put the actions in boxes.
o Join the boxes with arrows to show the right order.

Program
 A series of commands which tell the computer what to do.
 A program plan sets out the inputs, processes, and outputs of a program.
P a g e | 10

 eg/Plan for the quiz program


o Input: Ask a quiz question and get the answer.
o Process: Check if the answer is right
o Output: Tell the user if the answer is right

Machine Code
 The computer uses a language called machine code. The machine code is the only
language the computer can understand. Machine code is made entirely of numbers.
 Machine code is very hard for a human programmer to read and write.

Executable File
 A file made of machine code is called an executable file. Eg. .exe file
 Execute means carry out a command.
 An executable file is a file with commands that the computer can carry out.
 All the software on your computer is made of executable code.
 When your computer runs an application (app), it carries out all the commands
in the executable file.

Source Code
 A set of commands that a programmer writes in a programming language is called
source code.
 Converting a program from a programming language to machine code is called
translating the program.
 There are two ways to translate from a programming language to machine code:
compiling and interpreting.
 The process of turning a program into an executable file is called compiling. To
compile a program, you need a piece of software called a compiler.
 Interpreting works like this:
o The computer reads one command from the program.
o The computer translates and executes the command right away.
o The computer moves on to the next command.
 When a program is interpreted, the computer does not store any machine code.

Program Problems
All programmers make errors when they write programs.
P a g e | 11

Syntax Errors
Every programming language has rules. The rules of a language are called syntax. If
you break the rules of programming language, you make a syntax error.
When you run a program,
 Translating: The computer turns the commands into machine code.
 Running: The computer executes the machine code commands.
If there is a syntax error, the computer cannot understand the commands. It cannot
translate them into machine code. The computer will stop, and it will show you an error
message as followings:

Logical Errors
A logical error means that the logic of the prgram is wrong. The program works but it
does the wrong thing. It does not meet the requirement. It can be hard to spot a logica error
because:
 You can run the program.
 The computer executes all the commands.
 You don’t see an error message.

You might also like