0% found this document useful (0 votes)
3 views7 pages

Class 7 Python notes

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Delhi Public School, Hyderabad

Class 7
Python Introduction

What is Python?
Python is a popular programming language. It was created by Guido van Rossum,
and released in 1991.

It is used for:
 web development (server-side)

 software development

 mathematics

 system scripting

What can Python do?


 Python can be used on a server to create web applications.
 Python can connect to database systems. It can also read and modify files.
 Python can be used to handle big data and perform complex mathematics.
 Python can be used for rapid prototyping or for production-ready
software development.

Why Python?
 Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
 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 runs on an interpreted system, meaning that code can be executed as
soon as it is written.

Good to know
 The most recent major version of Python is Python 3 However, Python 2,
although not being updated with anything other than security updates, is still
quite popular.
 It is possible to write Python in an Integrated Development Environment such
as Thonny, Pycharm, Netbeans or Eclipse, which are particularly useful when
managing larger collections of Python files.
Python Comments
 Comments can be used to explain Python code.

Page 1 of 7
 Comments can be used to make the code more readable.

Creating a Comments in Python

Single line comments


Single line comments start with a #, and Python will ignore them:
Example
#This is a comment
print("Hello, world!")
Multiline Comments
Multiline comments in Python refer to a block of text or statements that are used for
explanatory or documentation purposes within the code. Unlike single-line comments
denoted by the hash symbol (#), multiline comments are enclosed by triple double quotes
(”””) or triple single quotes (”’). These comments are often utilized to provide detailed
explanations, documentation, or notes about the code, and they can span multiple lines.

Example
"""
This is a comment
written in
more than just one
line """
print("Hello, World!")
Python Keywords
Every language contains words and a set of rules that would make a sentence
meaningful. Similarly, in Python programming language, there are a set of predefined
words, called Keywords, which along with Identifiers will form meaningful sentences
when used together.
Python Keywords are some predefined and reserved words in Python that have
special meanings.
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await',
'break', 'class', 'continue', 'def', 'del','elif', 'else', 'except',
'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is']

Python Operators
Operators are used to perform operations on values

Example:
Python divides the operators in the following groups

Page 2 of 7
(Since it is the beginning, lets learn the following operators for now and we will keep
progressing as we learn the programming in depth)

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators

Arithmetic operators are used with numeric values to perform common mathematical
operations

Operator Name Example


+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y

Let us learn these operators listed above with a sample example code:
a=20
b=8
print(a+b) # output is 28
print(a-b) # output is 12
print(a*b) # output is 160
print(a/b) # output is 2.5
print(a%b) # output is the remainder of division i.e 4

Assignment Operators
Assignment operators are used to assign values to variables:

Operator Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3

Page 3 of 7
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

Logical Operators
Logical operators are used to combine conditional statements:

Operator Description Example

and Returns True if both statements are true x < 5 and x< 10

or Returns True if one of the statements is true x < 5 or x < 4

not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

Algorithms and Flowcharts


Algorithms and flowcharts are different mechanisms used for designing different
programs, particularly in computer programming. An algorithm is a step-by-step
summary of the procedure, while on the other hand, a flowchart illustrates the steps
of a program graphically.

Algorithm for finding the greatest of two numbers


Step 1: start
Step 2: input the values of A, B and Compare A and B.
Step 3: If A > B then go to step 5
Step 4: print “B is largest” go to Step 6
Step 5: print “A is largest”
Step 6: stop

Page 4 of 7
Flowchart Symbols:

Flowchart to find the greatest of 2 numbers

Python print() Function


The print() function prints the specified message to the screen or other standard
output device.
 print("Hello, world!") # printing a text message
 print(145) # printing a number

Python input() Function

The input () is a built-in function in python that retrieves the input from the user. For
example: If you want to determine whether the candidate is eligible to vote or not,
the input() is used by the user to enter his age. The input is entered by the keyboard
and is used to receive information from the user.

Page 5 of 7
 name = input("Enter your name: ")
 age=input(“Enter your age: ”)

Basic Python Data Types


Numeric types such as int, float, and complex
The str data type which represents textual data
Int
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
Examples
x=1
y = 35656222554887711
z = -3255522
Float
float, or "floating point number" is a number, positive or negative, containing one or more
decimals.
Examples
x = 1.10
y = 1.0
z = -35.59
Strings
Strings in python are surrounded by either single quotation marks, or double quotation
marks.
 'hello' is the same as "hello"
You can display a string literal with the print() function:
Example
print("Hello")

Python Variables

Variables
Variables are containers for storing data values.
Creating Variables
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.

Page 6 of 7
Example
x=4 # x is of type int
x = "Sally" # x is now of type string
print(x)

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)
 A variable name cannot be any of the Python keywords.

Valid variable names

myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

Invalid variable names

2myvar = "John" # variable name cannot start with a number


my-var = "John" # variable name cannot have special character
my var = "John" # Variable name cannot have spaces

Bibliography:

Byte Code by Kips Publications

Page 7 of 7

You might also like