[go: up one dir, main page]

0% found this document useful (0 votes)
41 views20 pages

Gr-10 Computer Sci Textbook-1-20

The document provides an overview of Python, including its history, scope, and applications, as well as the differences between programming and scripting languages. It covers the installation process, basic syntax, data types, and operators in Python, along with examples of writing and executing Python code. Additionally, it introduces loops and conditional statements, explaining their structure and usage in programming.

Uploaded by

gomathi
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)
41 views20 pages

Gr-10 Computer Sci Textbook-1-20

The document provides an overview of Python, including its history, scope, and applications, as well as the differences between programming and scripting languages. It covers the installation process, basic syntax, data types, and operators in Python, along with examples of writing and executing Python code. Additionally, it introduces loops and conditional statements, explaining their structure and usage in programming.

Uploaded by

gomathi
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/ 20

BVM GLOBAL GROUP OF SCHOOLS

CLASS: X
COMPUTER SCIENCE
Chapter - 1
PYTHON

 What is Python…?
 Differences between program and scripting language
 History of Python
 Scope of Python
 What can I do with python
 Who uses python today
 Why do people use Python?
 Installing Python IDE
 A Sample Code
 Python code execution
 Running Python

 Python Basic (Variable, Strings, Data types etc.)

What is Python…?
Python is a general purpose programming language that is often applied inscripting
roles.
So, Python is programming language as well as scripting language.
Python is also called as Interpreted language.

Differences between program and scripting language


Program Scripting
A program is executed (i.e. the  A script is interpreted.
source is first compiled and theresult
of the compilation is expected.  A "script" is code written in a
scripting language. A scripting
A "program" in general, is a sequence language is nothing but a type of
of instructions written so that a programming languae inwhich we can
computercan perform certain task. write code to control another software
application.
History:
 Invented in the Netherlands, early 90s by Guido van Rossum
 Python was conceived in the late 1980s and its implementation was started in
December 1989
 Guido Van Rossum is fan of ‘Monty Python’s Flying Circus’, this is a famous TV show in
Netherlands
 Named after Monty Python
 Open sourced from the beginning

Python’s Benevolent Dictator for Life:


“Python is an experiment in how much freedom programmers need. Too much freedom and
nobody can read another’s code; too little and expressiveness is endangered.”

— Guido van Rossum


Why was python created?
“My original motivation for creating Python was the perceived need for a high level language in the
Amoeba[Operating System] project.
I realized that the development of system administration utilities in C was taking too long. Moreover, doing these
things in the Bourne shell wouldn’t work for a variety of reasons..
So, there was a need for language that would bridge the gap between C and the shell”
-Guido van Rossum

Scope of Python:
Science
- Bioinformatics
System
Administration
-Unix
-Web logic
-Web sphere
Web Application Development
-CGI
-Jython –
Servlets Testing
scripts

What can I do with Python…?


 System programming
 Graphical User Interface Programming
 Internet Scripting
 Component Integration
 Database Programming
 Gaming, Images, XML , Robot and more

Who uses python today…


 Python is being applied in real revenue-generating products
 by real companies. For instance:
 Google makes extensive use of Python in its web search system, and employs Python’s creator.
 Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and IBM
 use Python for hardware testing.
 ESRI uses Python as an end-user customization tool for its popular GIS mapping products.
 The YouTube video sharing service is largely written in Python

Why do people use Python…?


The following primary factors cited by Python users seem to be these:
• Python is object-oriented

Structure supports such concepts as polymorphism,operation overloading, and multiple


inheritance.
• It's free (open source)

Downloading and installing Python is free and easy Source code is easily accessible
It's powerful:
 Dynamic typing
 Built-in types and tools
 Library utilities
 Third party utilities (e.g. Numeric, NumPy, SciPy) Automatic memory management
It's portable
 Python runs virtually every major platform used today
 As long as you have compatible Python interpreter installed, Python programs will run in
exactly the same manner, irrespective of platform.
Installing Python
 Python is pre-installed on most Unix systems, including Linux and MAC OS X
 But for in Windows Operating Systems , user can download from the
https://www.python.org/downloads/
 from the above link download latest version of python IDE and install, recent version is
3.6.8, 3.4.1 but most of them uses version 2.7.7 only
1. Download Python distribution
 You can download python distribution from the link given below
https://www.python.org/downloads/

 Note –Download only that python distribution/MSI Installer, which is best suited for the
Operating system on which you want to install it.
2. Python installation process
 Double-click the icon labeling the file<version>.exe
 Popup window will appear

 Click on Run option


3. Setup popup window will appear
If the Python Installer finds an earlier version of Python installed on your computer, the Install
Now message will ,instead appear as Upgrade Now(and the checkboxes will not appear).
4. User Account Control pop-up window will appear

 Click the Yes button.

5. A new Python <version> Setup pop-up window will appear with a Setup Progress
message and a progress bar.

 Click the Close button.


6. Soon, a new Python <version> Setup pop-up window will appear with a Setup was
successfully message

 Click the Close button.


Process of writing program:
 After installation of python ,we can work on it in following ways
(i) Interactive mode
(ii) Script mode

How to work in Python


(i) Interactive mode
1. Click start button All programs Python 3.6x IDLE (Python GUI)
(or)

How to work in Python


(i) in Interactive mode

• Click start button All programs Python 3.6x IDLE (Python GUI)
(or)

• Click start button All programs Python 3.6x Python (Command line)
2. In Python shell window (python command prompt appears as >>>)
3.Type commands in front of the Python prompt and Python will immediately give the result.

 In Interactive mode ,instructions are given in front of python prompt in python shell.
 Interactive mode does not save commands in form of a program.
 It is suitable for testing code.
 Type commands in front of the Python prompt and Python will immediately give the result.

(ii) in Script mode


1.Create a module/script/Program File
In python shell click File New

1. Create a module/script/Program File


a. Click Start button->All Programs à Python 3.6x àIDLE
b. Now click File->New in IDLE Python
Shell
Now type commands to save in the form of a
program
Eg:
print (“hello”)
print(“python”)
c. Click FileSave and then save the file with filename
and .py extension

2. Run module/Script/Program file


a. Click File Open and select the file you have already saved
b. Click Run-> Run Module
c. It will execute all the commands of program file and display
output in separate python shell window

OPERATOR IN PYTHON
Arithmetic Operators:
• Basic Arithmetic Operators are:
• +
• -
• *
• /
• //
• %
• **
The division operator /
The /operator always returns the result as float value

• 12/26.0

• 100/1010.0

• 7/2.52.8

The Floor division operator //


• The //operator always returns the whole part of the result after division.

• The fractional part is truncated.

• 12//26

• 100//1010

• 100.0//1010.0

The Modulus operator %


The %operator always returns the remainder.
• 12%52

• 100%100

• 7.2%31.2

The Exponentiation operator **


The ** operator performs exponentiation.

• 4**364

• 36**.56.0

• 10.0**201e+20

Relational Operators
The relational operator compares two expression and results in either True or False.

• <

• >
• <=

• >=

• ==
• !=

Logical Operators
• While relational operators establishes the relationship among values, logical operators refers to
the ways these relationships can be connected.Python provides three logical operators to
combine existing expressions. These are:

• and

• not

• or

VARIABLES IN PYTHON
Variable
Create a Variable:
>>>headmaster=“Dumbledore”
>>>print (headmaster)
Dumbledore

Assigning a New Value:


>>>headmaster=“Hardcastle”

>>>print (headmaster)
Hardcastle

DATA TYPES IN PYTHON


Data Type:
Python has many native data types. Here are the important ones:
o Booleans are either True or False.
o Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions
(1/2 and 2/3), or even complex numbers.
o Strings are sequences of Unicode characters, e.g. an HTML
document.
o Bytes and byte arrays, e.g. a JPEG image file.
o Lists are ordered sequences of values.
o Tuples are ordered, immutable sequences of values.
o Sets are unordered bags of values.
Example:

o String “Whoop”

o Integer 42
o Float 3.14159

o List [“John”, “Paul”, “George”, “Ringo”]

o Python can tell us about types using the type() function:


>>>print type(“Whoop!”)

<type ‘str’>

STRINGS IN PYTHON
Strings
Lists
Index: Where an item is in the list
>>> Beatles = [“John”, “Paul”, “George”, “Ringo”]

>>> Beatles[0]

‘John‘

[“John”, “Paul”, “George”, “Ringo”]

0 1 2 3

Python index starts from zero

S.NO Function with Description

1 sum(list) Returns the sum of the given list.

2 len(list) Gives the total length of the list.


3 max(list) Returns item from the list with max value.

4 min(list) Returns item from the list with min value.


5 list(seq) Converts a tuple into list.

TUPLE: DATA TYPE


Tuples:
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences
between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses,
whereas lists use square brackets.

Built-in Tuple Functions


Python includes the following tuple functions −
S.NO Function with Description

1 sum(tuple) Returns the sum of the given tuple.

2 len(tuple ) Gives the total length of the tuple.


3 max(tuple) Returns item from the tuple with max value.
4 min( tuple) Returns item from the tuple with min value.
5 tuple(seq) Converts a list into tuple.

Practical Notes
1. Simple Programs
2. creating python with .PY extension
3. Odd or Even number
4. Print Natural Numbers
5. WAP to find person is eligible to vote or not.
6. WAP to print 5,10,15 tables
7. Uses of different types of variable in Python
Chapter 2
LOOPS & CONDITIONAL STATEMENTS

Statement Description
if statements An if statement consists of a boolean expression
followed by one or more statements.

if...else statements An if statement can be followed by an


optional else statement, which executes when
the boolean expression is FALSE.

nested if statements You can use one if or else if statement inside


another if or else if statement(s).

Loop type
Loop Type Description
while loop Repeats a statement or group of statements
while a given condition is TRUE. It tests the
condition before executing the loop body.
For loop Executes a sequence of statements multiple
times and abbreviates the code that manages the
loop variable.
Nested loop You can use one or more loop inside anyanother
while, for or do..while loop.
CONDITIONAL STATEMENTS (Decision Making)

• The basic decision statements in computer is selection structure.

• The decision is described to computer as a conditional statement that can be answered


True or False.
Python language provide the following conditional (Decision making) statements.

• if statement

• if...else statement

• if...elif...else staement

• Nested if..else statement

The if statement
The if statement is a decision-making statement. It is used to control the flow of execution of
thestatements and also used to test logically whether the condition is true or false.

Syntax

if test expression:

statement(s)

Example program
i =int(input(“Enter the number:”))
if(i<10):
print(“condiotion is true”)

OUTPUT

Enter the number: 9

Condition is true
If … else statement
The if…else statement is called alternative execution, in which there are two possibilities and the
condition determines which one gets executed.

Syntax

if test expression:

Body of if

else:

Body of else

Write a program to check if a number is odd or


even
num = int(input(“Enter the number:”)) if
(num % 2)== 0:)
print (“Given number is Even”)else:
OUTPUT
print(“ Given number is odd”)
Enter the number: 9
Given number is Odd
elif Statements:
 Elif – is a keyword used in Python in replacement of else if to place another in the program.
This is called chained conditional.
 Chained conditions allows than two possibilities and need more than two branches.

SYNTAX

If expression:
Body of if
elif
expression:
Body of elif
else:
Body of else
Figure – elif condition Flowchart:

Example: largest among three numbers


a=int(input(“Enter 1st number:”)) OUTPUT
b=int(input(“Enter 2nd number:”)) Enter1st number:10
c=int(input(“Enter 3rd number:”))
Enter 2nd number:25
if (a > b) and (a > c):
print("a is greater") elif Enter 3rd number:15
(b < a) and (b < c):
b is greater
print(“b is greater")
else:
print(“c is greater")

Nested if … else Statements:


 We can write an entire if….else statement in another if… else statement called nesting, and the
statement is called nested if.
 In a nested if construct, you can have an if … elif… else construct inside an if … elif.. Else
construct.
Syntax

if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)

Example program
n= int(input(“Enter
if(n<=15):
if (n == 10):
OUTPUT
print(‘play cricket’)
Enter number:10
else:
print(‘play kabadi’) play cricket

else:
print(‘Don’t play game’)

CONTROL STATEMENT (Looping Statement)


 Program statement are executed sequentially one after another. In some situations, a block of
code needs of times.

 These are repetitive program codes, the computer have to perform to complete tasks. The
following are the loop structures available in python.

 while statement

 for loop statement

 Nested loop staement


While loop statement:
 A while loop statement in Python programming language repeatedly executes a target
statement as long as given condition is true.
Syntax of while loop

while expression:
statement(s)

Write a program to find sum of number

num = int(input("Enter a number”))


sum = 0
while(num > 0): OUTPUT

sum = sum+num Enter a number:10

num = num-1 The sum is:55

print("The sum is:”, sum)

For loop statement


 The for loop is another repetitive control structure, and is used to execute a set of instructions
repeatedly, until the condition becomes false.

 The for loop in Python is used to iterate over a sequence (list, tuple, string) or
other iterable objects. Iterating over a sequence is called traversal.

Syntax
for val in sequence:
Body of for loop

For loop flow chart


Addition of number using for loop

numbers = [6, 5, 3,8,4,2,5,4]


sum1 = 0
for val in numbers:

sum1 = sum1+val OUTPUT

print("The sum is:”, sum1) The sum is 37


Jump statements in Python
Jump statements in Python are used to alter the flow of a loop to skip a part of a loop orterminate a loop.

1. break – It is used to exit from the current loop.


2. continue – It is used to skip all the remaining statements in the loop and move controls
back to the top of the loop.

Break statement

Continue statement
Pass statement – It is an empty statement do nothing. It is generally used as a placeholder.

Using else statement with while loops


Python supports t have an else statement associated with a loop statement.
If the else statement is used with a while loop, the else statement is executed when the
condition FALSE

Program to illustrate the else in while loop


counter = 0
while counter < 3: print("Inside OUTPUT

loop") counter = counter +1 Inside loop


Inside loop
else:
Inside loop
print(“Outside loop")
Outside loop

for Loop and for Loop with else


EX-01:
genre = ['pop', 'rock', 'jazz'] for i OUTPUT
in range(len(genre)): I like pop I like rock I
print ("I like",genre[i]) like jazz

You might also like