Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

ICT Final Exam Prep - Intoduction To Python & More About Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

ICT Final Exam Prep - Intoduction To Python & More About Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)

Chapter 07 – Introduction to Python & Chapter 08 – More About Python

Python:

Is high-level programming language

Is object oriented

Is open source, meaning its code is available freely

Was developed in 1991 by Guido Van Rossum

Is owned by Python Software Foundation (PSF)

Supports Graphic User Interface (GUI)

Process of repeating a line of code is called LOOP or ITERATION

Operators are special symbols used to tell a language translator what to do with variables and values
There are four kinds of operators in Python. Details of each type are listed below:

Arithmetic Operators Used to perform math function on two variables


a + b Add 'a' and 'b'
a -b Subtract 'b' from 'a'
a *b Multiply 'a' and 'b'
a /b Divide 'a' by 'b'
a % b Divide 'a' by 'b' and give remainder only (ignore answer)
a // b Divide 'a' by 'b' and give answer only (ignore remainder)
a ** b 'a' raised to power 'b'
Page 1 of 7
ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)
Chapter 07 – Introduction to Python & Chapter 08 – More About Python

Assignment Operators Used to assign new values to variables


a = b Make 'a' equal to 'b'
a += b Increase 'a' by 'b’ or add 'b' to 'a'
a -= b Decrease 'a' by 'b' or subtract 'b' from 'a'
a *= b Multiply 'a' by 'b'
a /= b Divide 'a' by 'b'
a //= b Divide 'a' by 'b'. Give answer only (ignore remainder)
a %= b Divide 'a' by 'b'. Give remainder only (ignore the answer)
a **= b 'a' raised to power 'b'

Logical Operators Used to combine conditions


a < b and a == c Carry out instruction if 'a' is less than 'b' and also if 'a' is equal to 'c'
a < b or a == b Carry out instruction if 'a' is either less than 'b' or 'a' is equal to 'b'
not (a == b) Carry out instruction only if 'a' is not equal to 'b'

Relational Operators Used to check the relation between two variables


a == b Is 'a' equal to 'b' ?
a != b Is 'a' NOT equal to 'b' ?
a > b Is 'a' greater than 'b' ?
a < b Is 'a' less than 'b' ?
a >= b Is 'a' greater than OR equal to 'b' ?
a <= b Is 'a' less than OR equal to 'b' ?

Page 2 of 7
ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)
Chapter 07 – Introduction to Python & Chapter 08 – More About Python

Following are some RULES and some FACTS about Python

Rules for choosing variable names in Python


Variable names in Python:
Are case sensitive (Num and num are not the same)
Can have letters, digits and underscore
Cannot start with underscore
Cannot have spaces in them
Cannot be Python keywords (like print, if, else, input, int etc)

Rules for declaring variables and assigning values to variables


In Python can:
assign value directly (example: a=3, since 3 is an integer, 'a' is declared as integer with value 3
assign values to multiple variables together (example: a,b,c = 1,2,3; this means 'a' = 1, 'b' = 2 and 'c' = 3)
assign same value to multiple variables at the same time (example: a=b=c=25, means 'a' = 25, 'b' = 25 and 'c' = 25)

Page 3 of 7
ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)
Chapter 07 – Introduction to Python & Chapter 08 – More About Python

Commad Statements in Python


Commands are keywords used in the Python. Each have a specific function. Hense they are also called 'functions'.
Function names cannot be used to name variables
Simple Fuctions : They are executed directly
Command Uses Syntax Effect
Used when input is required from the user.
"Type your name" will appear on screen and will stay
User's input is saved as a string. Program input("Type your name")
till the user types something and presses Enter
input() flow stops till user provides his input.

User's answer to this question is saved by the Python


User's input can be saved in a variable Name = input("Type your name")
program under the variable Name

"Type your age" will appear on the screen and will


Used when user's input has to be converted
int() int(input("Type your age")) stay until user types his age and presses Enter. The
to integer from string
age will be saved as an integer

Used to display something on the screen. "Welcome to Python" will appear on the screen. After
print() Program flow returns to next statement in print("Welcome to Python") that the next instruction in the python program will
the Python code be executed

Page 4 of 7
ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)
Chapter 07 – Introduction to Python & Chapter 08 – More About Python

Conditional Functions : They are only executed when required conditions are met

Command Uses Syntax Effect

Used if a condition should be checked before


if a<b: If value of 'a' is less than the value of 'b', "a is less
executing the next instruction in the python
than b" will appear on the screen. If 'a' is not less
if code. if statement always ends with a ":".
than 'b', this sentence will not appear and program
Command under if statement should have an
will go to next instruction in the python code
indent before them print("a is smaller")

If value of 'a' is less than the value of 'b', "a is less


Used when only one of the two actions if a<b: than b" will appear on the screen. If 'a' is not less
should be taken. (when deciding which of the than 'b', this sentence will not appear and program
two actions to take). if and else both should will go to next instruction in the python code
if - else print("a is smaller")
end with ":". Commands under if and else
statements should have an indent before If value of 'a' is not less than the value of 'b', "a is not
them else a>=b:
smaller" will appear on the screen. Program will go to
print("a is not smaller") next instruction in the python code

If value of 'a' is less than the value of 'b', "a is


if a<b: smaller" will appear on the screen. If 'a' is not less
Used when only one of many actions should than 'b', this sentence will not appear and program
be taken. (when deciding which one of the will check the next condition
many actions to take). if, elif and else should print("a is smaller")
if-elif-else
end with ":". Commands under if, elif and
else statements should have an indent If value of 'a' is bigger than the value of 'b', "a is
elif a>b: bigger" will appear on the screen. If 'a' is not bigger
before them
than 'b', this sentence will not appear and program
will check the next condition
print("a is bigger")
Page 5 of 7
ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)
Chapter 07 – Introduction to Python & Chapter 08 – More About Python
If value of 'a' is equal to the value of 'b', "a is equal to
else a==b: b" will appear on the screen. If 'a' is not equal to 'b',
this sentence will not appear and program will exit
print("a is equal to b") the if-elif-else segment of the code

Loop Fuctions : They keep being executed untill the required conditions become false
Command Uses Syntax Effect
range() is a counting function. It uses a variable 'a' to
keep track of number of iterations. 'a' starts from the
Used to repeat a part of Python code by a first number (1) in the range() function. After each
desired number of times. Number of for a in range(1,4,2): iteration of the for loop, value of 'a' increases by the
iterations can be defined using the range() third number (2) in the range() function. for loop is
for function as shown. for loops are useful when exited when value of 'a' reaches the middle number
number of iterations are known. for (4) in the range() function. So for this example output
statements should end with ":". Commands print("a=", a) would be: (following will appear on the screen)
under for statements should have an indent
a=1
a=3
Value of variable 'b' is set in the bigging as 3. Value of
variable 'a' can be set too. while statement will
continue to be executed as long as the value of
Used to repeat a part of Python code variable 'a' stays less than 'b'. For this example, value
multiple times but the required number of a=1, b=3
of 'b' is known, but if the value of 'b' was being
iterations are not known. while statements
while entered by the user, the value would not be known.
should end with ":". Commands under while while a<b: The while statement would be very useful then. For
statements should have an indent before this example, output would be:
print("a=", a)
them
a=a+1
a=1
a=2

Page 6 of 7
ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)
Chapter 07 – Introduction to Python & Chapter 08 – More About Python

Jump Statements : Python has two jump statements. They enable you to restart or come out of loops (iterations)

a=1, b=3 When the value of variable ‘a' becomes more than
the value of variable 'b', the if loop will not be
Used to jump out or "break out" of the loop executed. At that point the statement right after the
or iteration of code. When the break if a>b: if loop will be executed. The output for this example
break statement is executed the program flow goes will be:
to the very next Python code line that is after break a=1
the loop.
print("a=", a) a=2
a=a+1 a=3
print("All done") All done

a=1, b=4 Values of variables 'a' and 'b' are set. Each iteration
Used to jump back in and start the next next while a<b: of the while statement increases the value of 'a' by
iteration of a loop if a condition is true. When '1'. If 'a' becomes equal to 'b' while loop is started
a=a+1 again till the condition a<b is no longer true.
the continue statement is executed the if a==b
continue Outcome for the above code will be:
program flow ignore the remaining
instruction in the loop and starts the loop continue
again. print("a=", a) a=2
print("All done") a=3
All done

Good Luck Everyone Hope this helps

Page 7 of 7
int( ) is a Python ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)
function. It will
= is an convert the
X is a variable. When user assignment user’s input to an input( ) is a What is your
types his age, it will be operator. It is integer. Anything typed
Python function. name? This
saved as X assigning the Otherwise user’s between the “ ”
It stops the sentence will
user’s input (age) input (age) will will be displayed
program control appear on the
to the variable X be considered a on the screen as
flow until user screen as it is
string it is
enters his input written between
(age) double quotes

X=int(input(“What is your age?”))


print(“You are“, X, “years old“)

Different parts of a
Value of X was
sentence should be
print() function Everything saved in the
Everything separated by
will cause the written between computer
written in ““ will commas ‘,’.
items between double quotes memory because
appear on the Everything between
the brackets ( ) to “ “ will appear on of the input()
screen as it is “ “ will be displayed
appear on the the screen as it is function. It will
as is. Value of ‘X’
screen be taken from
will be taken from
there and printed
computer memory
ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)
When this line of Python code is
run the following message
appears on the screen. The
program waits for the user to X=int(input(“What is your age?”))
type a value.
User’s answer (15) is saved as an integer. Now
math operations can be performed on it.

What is your age? 15


When the user types ‘15’ and
presses Enter, his answer (15) is
saved in the computer memory
under the variable ‘X’
X = 15

Computer Screen Computer Memory


ICT Final Exam Prep Notes (by Palwasha Ahmed 8A-1)
When this line of Python code is run
the following message appears on the
screen. The program waits for the
user to type a value because of X=int(input(“What is your age?”))
input() function. Next line of code User’s answer (15) is saved as
does not run unless user type his
an integer because of the int()
answer (15) and presses Enter
function. Now math operations
= is an assignment operator. User’s can be performed on it.
answer is being assigned to
memory location X
What is your age? 15
You are 15 year old When the user types ‘15’ and presses
Enter, his answer (15) is saved in the
computer memory under the variable ‘X’
X = 15
print() function below will cause the
following message to appear on the
screen. It will take the value of ‘X’ from
the memory.

Computer Screen
print(“You are“, X, “years old“) Computer Memory

You might also like