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

T - Grade 8 - Python Notes

Python is a high-level, easy-to-learn programming language known for its readability and simplicity, making it suitable for beginners. It can be used for various applications including web development, data analysis, and automation, with a syntax that allows for concise code. Key features include dynamic typing, indentation for code blocks, and built-in functions for input and output.

Uploaded by

robloxmailid
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)
2 views10 pages

T - Grade 8 - Python Notes

Python is a high-level, easy-to-learn programming language known for its readability and simplicity, making it suitable for beginners. It can be used for various applications including web development, data analysis, and automation, with a syntax that allows for concise code. Key features include dynamic typing, indentation for code blocks, and built-in functions for input and output.

Uploaded by

robloxmailid
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
You are on page 1/ 10

Python programming language

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.

Guido Van Rossum created python when he was working at CWI


(Centrum Wiskunde & Informatica) which is a National Research
Institute for Mathematics and Computer Science in Netherlands. The
language was released in I991. Python got its name from a BBC
comedy series from seventies- “Monty Python‟s Flying Circus” not
after python the snake.

What can Python do?


 Python can be used on a server to create web applications.
 Python can be used alongside software to create workflows.
 Python can connect to database systems. It can also read and modify
files.
 Python can be used to perform complex mathematics.

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

Python print() Function:


The print() function prints the specified message to the screen.

Example:
print("Hello World")

print("Hello", "how are you?")


To combine both text and a variable, Python uses the + character:
Example
x = "awesome"
print("Python is " + x)
You can also use the + character to add a variable to another variable:
Example
x = "Python is "
y = "awesome"
z=x+y
print(z)
For numbers, the + character works as a mathematical operator:
Example
x=5
y = 10
print(x + y)

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))

Python input() Function:


The input() function allows user to enter input and stores the value in a
variable.

Example:
x = input('Enter your name:')
Python programs:
Program to print few lines as output:
print("Welcome to Python programming..")

Output:

Welcome to Python programming..

Example2:

name=input(“Enter your name “)

Enter your name Jay

print("Hello " + name)

Output:

Hello Jay

Program to read marks of a student and print the total:


eng=input("Enter your marks in English ")

Enter your marks in English 75

eng=int(eng)

math=input("Enter your marks in Math ")

Enter your marks in Math 99

math=int(math)

sci=input("Enter your marks in Science ")

Enter your marks in Science 97

sci=int(sci)

soc=input("Enter your marks in Social ")

Enter your marks in Social 98

soc=int(soc)

total=eng+math+sci+soc

print("Total marks are " , total)

Output:
Total marks are 369

Python Comparison Operators


Comparison operators are used to compare two values:

Operator Name Example

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

Python Conditions and if statements


Python supports the usual logical conditions from mathematics:

 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

These conditions can be used in several ways, most commonly in "if


statements"

An "if statement" is written by using the if keyword.

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")

You can also have an else without the elif:

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

Program to read marks of a student and print his grade:


marks=input("Enter your marks ")
Enter your marks 75
marks=int(marks)
if marks >=90:
print("A grade")
elif marks>=80:
print("B grade")
elif marks>=70:
print("C grade")
elif marks>=60:
print("D grade")
elif marks>=50:
print("E grade")
elif marks>=40:
print("F grade")
else:
print("No grade")

Output:

C grade

You might also like