T - Grade 8 - Python Notes
T - Grade 8 - Python Notes
Contents
What is Python? ................................................................................................................................ 2
Introduction ....................................................................................................................................... 2
What can Python do? ....................................................................................................................... 2
Why Python? ..................................................................................................................................... 2
Python Syntax ................................................................................................................................... 3
Python Indentations ......................................................................................................................... 3
Python Comments ............................................................................................................................ 3
Docstrings .......................................................................................................................................... 3
Creating Variables ............................................................................................................................ 3
Example: ......................................................................................................................................... 4
Variable Names ................................................................................................................................. 4
Python print() Function: ................................................................................................................... 4
Type .................................................................................................................................................... 5
Python Casting .................................................................................................................................. 5
Data type ........................................................................................................................................... 5
Python Numbers ............................................................................................................................... 5
int ........................................................................................................................................................ 6
float..................................................................................................................................................... 6
Complex ............................................................................................................................................. 6
Python input() Function: .................................................................................................................. 6
Python programs: ............................................................................................................................. 7
Python Comparison Operators ....................................................................................................... 8
Python Conditions and if statements............................................................................................. 8
Example of if statement: .......................................................................................................... 8
elif statement:.................................................................................................................................... 9
else statement: .................................................................................................................................. 9
Python programs: ........................................................................................................................... 10
What is Python?
Python is an easy-to-learn programming language that has some useful
features for a beginning programmer. The code is quite easy to read when
compared to other programming languages, and it has an interactive shell
into which you can enter your programs and see them run. Python is a
widely used high-level, general-purpose, interpreted, dynamic programming
language. Its design philosophy emphasizes code readability, and its syntax
allows programmers to express concepts in fewer lines of code than would be
possible in languages such as C++ or Java. The language provides constructs
intended to enable clear programs on both a small and large scale.
Introduction
A computer program is a set of instructions that causes a computer to
perform some kind of action. It is not the physical parts of a computer—like
the wires, microchips, cards, hard drive, and such—but the hidden stuff
running on that hardware. Like humans, computers use multiple languages
to communicate— in this case, programming languages. A programming
language is simply a particular way to talk to a computer—a way to use
instructions that both humans and the computer can understand. There are
programming languages named after people (like Ada and Pascal), those
named using simple acronyms (like BASIC and FORTRAN), and even a few
named after TV shows, like Python.
Why Python?
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
Python was designed to for readability, and has some similarities to
the English language with influence from mathematics.
Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
Python Syntax
Python syntax can be executed by writing directly in the editor Or by creating a python file
on the server, using the .py file extension, and running it in the editor
Python Indentations
Where in other programming languages the indentation in code is for readability only, in
Python the indentation is very important.
Python uses indentation to indicate a block of code.
Example
if 5 > 2:
print("Five is greater than two!")
Python will give you an error if you skip the indentation:
Python Comments
Python has commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment:
Example
Comments in Python:
#This is a comment.
print("Hello, World!")
Docstrings
Python also has extended documentation capability, called docstrings.
Docstrings can be one line, or multiline.
Python uses triple quotes at the beginning and end of the docstring:
Example
Docstrings are also comments:
"""This is a
multiline docstring."""
print("Hello, World!")
Creating Variables
Unlike other programming languages, Python has no command for declaring
a variable.
A variable is created the moment you first assign a value to it.
Example:
x = 5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type and can even change type after
they have been set.
Example
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-
9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
Remember that variables are case-sensitive
It is a good practice to follow these identifier naming conventions:
1. Variable name should be meaningful and short
2. Generally, they are written in lower case letters
Example:
print("Hello World")
Type
To verify the type of any object in Python, use the type() function:
Example
print(type(x))
print(type(y))
print(type(z))
Python Casting
There may be times when you want to specify a type on to a variable. This can be done with
casting.
Casting in python is therefore done using constructor functions:
int() - Converts, a float literal, or a string literal to an integer
float() - Converts, a float number from an integer literal, a float literal or a string
literal to a float
str() - Converts, wide variety of data types, including strings, integer literals and float
literals into strings
Data type
It is a set of values, and the allowable operations on those values.
It can be one of the following:
1. Number
2. Sequence
3. Sets
4. Mapping
5. Dictionaries
Python Numbers
There are three numeric types in Python:
int
float
complex
Variables of numeric types are created when you assign a value to them:
Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
int
int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
Example
Integers:
x=1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
float
float, or "floating point number" is a number, positive or negative, containing one or more
decimals.
Example
Floats:
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Complex
Complex numbers are written with a "j" as the imaginary part:
Example
Complex:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Example:
x = input('Enter your name:')
Python programs:
Program to print few lines as output:
print("Welcome to Python programming..")
Output:
Example2:
Output:
Hello Jay
eng=int(eng)
math=int(math)
sci=int(sci)
soc=int(soc)
total=eng+math+sci+soc
Output:
Total marks are 369
== Equal x == y
!= Not equal x != y
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Example of if statement:
a = 33
b = 200
if b > a:
print("b is greater than a")
elif statement:
The elif keyword is pythons way of saying "if the previous conditions were
not true, then try this condition".
Example:
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else statement:
The else keyword catches anything which isn't caught by the preceding
conditions.
Example:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Example:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Python programs:
Program to read two numbers and print the larger of two:
a=input("Enter a number")
Enter a number10
a=int(a)
b=input("Enter another number")
Enter another number25
b=int(b)
if a>b:
print(a)
else:
print(b)
Output:
25
Output:
C grade