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

Class 6th - Introduction To Python

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

Class 6th - Introduction To Python

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

What will you learn in this chapter?

 What is Python?  Variables in Python


 Modes of Python  Use of input () and print ()
 Datatypes in Python functions in python

Introduction

A program is written in a specific language called as Programming Language.


A person who writes a program is called a computer programmer. In order to perform
any task a programmer writes the set of instructions known as program using any
programming language.
In this chapter you are going to learn the basics of one of the most popular high-level
programming languages called Python.

Python

Python is easy to learn & powerful object-


oriented programming language. It is high level
programming language which is derived from
many other programming languages such as C,
C++, Java.

Do You Know Do You Know Do You Know

Python was created Python got its name Python is based on or


by Guido Van Rossum. after the famous BBC influenced from two
It was first released on Comedy Show “Monty programming
February 20, 1991. Python’s Flying languages:
Circus”. a) ABC b) Modula 3
Features of Python Language

Very easy to Platform An Case


learn independent Free to use interpreted sensitive
language

Applications of Python Language

Web
Applications

Business GUI-based
Applications Desktop
Applications

Python
Applications

Audio/Video Image
based Processing
Applications Applications

3D CAD
Applications

Starting Python

To begin programming in Python, we need to have Python interpreter installed in


the computer. The most commonly used Python development environment in GUI
integrated IDLE (Integrated Development Environment). We can write, edit, run,
browse and debug a Python program using this environment.
After downloading and installing Python IDLE on a computer system follow these steps
to start programming.
Step 1: Click on Start menu → Python 3.8 → IDLE (Python 3.9) Smart Tip
as shown in Fig 1. We can also double
click on the desktop
Step 2: The Python 3.9 Shell window appears (Fig 2) icon to start working
on Python.

Fig 1: Starting Python Fig 2: Python Shell Window


There are three greater-than signs ‘>>>’ also called primary prompt. This is used as
the prompt to begin a command in Python.

Modes in Python

Python IDLE provides two separate ways or modes to write and run Python programs.
These are:
 Interactive mode/Immediate mode/Shell mode
 Script mode/Editor mode
Interactive Mode
In this mode we write a single line of Python code and immediately see the results.
For example, type print (“Hello Everyone”) at the prompt and press Enter key. You
will see the output immediately as shown in Fig 3.

Fig 3: Using the Interactive Mode


However, the drawback of this mode is that you cannot save the statements for further
use.
Script Mode
In this mode we can write the code of multiple lines and execute it in one go. In this
mode we can save a code and use it again later.

Creating and saving a program in Python

To use script mode, follow these steps:


Step 1: In Python Shell window, click on File menu → New File
option. (Fig 4)
A new window appears. (Fig 5)
Step 2: Type the given code in this mode as shown in Fig 6.

Fig 4: Creating new file


in script mode

Fig 5: Script Mode Fig 6: Writing code in script mode


Step 3: Save the program by clicking on File
menu → Save option.
The save as dialog appears. (Fig 7)

Step 4: Select the folder where you want to


save your program. Type the name of your
program in File Name box. Click on Save
button.

Do You Know Fig 7: Save As dialog box

Your file will be saved


with extension .py
Executing /Running the Script mode

To run/execute a program in Python, follow these steps:

Click on Run menu → Run Module


OR

Press F5 key from keyboard.

Closing Python

To close Python window, follow these steps:

Click on File menu → Exit option


OR

Click on Close (x) option present on title bar.

Variable in Python

Variables are named storage locations where data is stored temporarily during program
execution. Values of variables can be changed during program run.
Rules for naming a variable:

Variable name can have letters or numbers.


Variable name must begin with a-z, A-Z or an underscore (_).
Spaces, special symbols or punctuation marks are not allowed in variable name.
Keywords cannot be used as variable name.
Declaring a variable
Python variables do not need a separate declaration to reserve the memory space. A
variable is created automatically when you assign a value to it. The equal (=) sign is
used to assign values to variables.
For example, Marks = 70.
Here, a variable Marks is created and value 70 is assigned to it.
Data Types in Python

The type of value that a variable holds is called its datatype. Following data types are
used in Python:
Integer
The integer data type contains positive or negative whole numbers with no decimal
point.
For example, A = 10, age = 15, b = -9
Float
The float data type represents real numbers and it is written with a decimal point.
For example, c = 4.5, x = 23.45
String
A string is a collection of characters enclosed in single quotes (‘ ’), double quotes (“ ”)
or triple quotes (‘‘‘ ’’’)
For example, name = “Sunil Khanna”, K = ‘hello’
Boolean
Boolean values are True and False. True is 1 and False is 0.
For example, s = 5 > 8, it will print false.

Practice Time

Identify the data types assigned to the following variables:


(a) a = 34 _____________
(b) b = 89.88 _____________
(c) c = “bye” _____________
(d) d = 4>2 _____________
(e) num = -6 _____________
Displaying output on screen

In Python, print () function is used to display an output on screen. It can also be used
for calculations, such as
Smart Tip
print(5+7)
Python is a case-
sensitive language. So,
Print() is invalid whereas
print() is valid command

Fig 8: Using print() function

Practice Time

Open Python in Shell mode and type the following commands. Write the output in given
space.
(a) 6 * 3 – 5 _______
(b) 40 / (20 – 16) * 2 _______
(c) 8 * (4 + 7) _______
(d) 13 + 7 – 3 * 4 – 2 _______

Taking input from user

The input () function is used to accept input from keyboard. This function takes the
value from the keyboard as a string argument.
Program – 1: A program to demonstrate the use of input() function.

Fig 9: Using input() function


Program – 2: A program to add two numbers.

Fig 10: Output


Fig 10: Program in script mode

Do You Know

Int() or float() function is used along with the input() function to convert
strings into integers or float values.

Practice Time

(a) Write a program to find average of three numbers.


(b) Write a program to find area and perimeter of rectangle.
(c) Write a program to find area of triangle. [Hint: Area of triangle =½ * (b * h)
(d) Write a program to input marks of five subjects and display total marks and
percentage.
~~~~ Check Your Progress ~~~~

A Multiple Choice Questions

1. Python is a platform __________ language.


(a) dependent (b) independent
(c) controlled (d) None of these
2. Which data type represents real numbers with a decimal point?
(a) Integer (b) Float
(c) Boolean (d) String
3. Which function is used to accept input from keyboard?
(a) input () (b) float ()
(c) print () (d) accept ()
4. Which of the following symbol is used to assign value to a variable?
(a) # (b) =
(c) == (d) >
5. Which function is used along with input () function to convert strings into
integers?
(a) input () (b) float ()
(c) int () (d) string ()

B One Word Answers

1. Developer of Python __________________


2. Python was first released on __________________
3. Function key to run python program __________________
4. Command prompt in python __________________
5. Extension of python program __________________

C Short Answers

1. Write any 4 features of Python language.


2. Write any 4 applications of computer.
3. Give two methods to execute a program in Python.
4. What are two methods to exit Python.
5. Write any 2 rules for naming a variable.

You might also like