[Go to site: main page, start]

0% found this document useful (0 votes)
1 views8 pages

Chapter2 Python Notes

Chapter 2 introduces Python programming, highlighting its features, advantages, and limitations. Python is an open-source, high-level, and versatile language, developed by Guido van Rossum in 1991, known for its readability and extensive libraries. The chapter also covers installation, interaction through Python IDLE, and differences between interactive and script modes.

Uploaded by

Aleena Fathima
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)
1 views8 pages

Chapter2 Python Notes

Chapter 2 introduces Python programming, highlighting its features, advantages, and limitations. Python is an open-source, high-level, and versatile language, developed by Guido van Rossum in 1991, known for its readability and extensive libraries. The chapter also covers installation, interaction through Python IDLE, and differences between interactive and script modes.

Uploaded by

Aleena Fathima
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

Chapter 2

Getting Started with Python

Class XI | Informatics Practices | Complete Study Notes

2.1 Introduction
A computer is a versatile machine that can execute millions of instructions per second, but can neither
think nor make decisions on its own. It requires a program to instruct it what needs to be done.
• Program: A set of instructions written in a sequence that guides the computer to perform a particular
task.
• Programming: The process of writing a program.
• The first and foremost step while writing a program is to understand the problem well, analyse it and
solve it logically.
• Writing correct code in the proper sequence and right context is extremely important — this is where
Computational Thinking comes into play.
• Before actual coding starts, the ways in which a problem could be resolved must be understood first.

CTM: Programming languages have been developed to meet the needs of programmers and
individual users. Languages die and become obsolete when they fall short of expectations, and are
replaced by more powerful languages. However, Python has stood the test of time and remained
relevant across industries.

2.2 Python Programming Language


The name 'Python' was chosen because just as the reptile has a flexible body, the language too is easy,
simple, and highly flexible.
• Python is an open-source, object-oriented, high-level programming language.
• Developed by Guido van Rossum in 1991 at the National Research Institute for Mathematics and
Computer Science, the Netherlands.
• Based on the ABC language — a teaching language created to replace BASIC.
• Developed using the C language.
• A general-purpose programming language that can build any kind of program that does not require
direct access to the computer's hardware.

CTM: Python was also inspired by the famous BBC comedy show Monty Python's Flying Circus.

2.2.1 Features of Python

Feature Description
Interpreted Processed at runtime by the interpreter; no need to compile before executing.

Interactive Supports direct command-line interaction; results appear immediately.

Object-Oriented Supports OOP concepts like classes and objects.

Loosely Typed No need to declare data types of variables explicitly; dynamically typed.

Free & Open-Source Freely available; source code can be modified and distributed.

Portable Runs on Windows, Linux/Unix, macOS and other platforms without change.

High-Level Uses English-like syntax; programmer does not manage memory directly.

Expressive Less code needed to express complex ideas; programs are 3–5x shorter than
equivalents in C++/Java.

Extensible Can be extended with modules written in C, C++, or other languages.

Embeddable Python code can be embedded inside C/C++ programs.

Large Standard Huge built-in library with modules for almost every task.
Library

GUI Programming Supports graphical applications via libraries like Tkinter.

Dynamically Typed Type of a variable is determined at runtime; faster to write code.

Garbage Collection Automatic memory management; unused memory is freed automatically.

2.3 Advantages of Python


• Platform-Independent: Runs on Windows, Linux/Unix, macOS and others — Python is a portable
language.
• Readability: Uses clear, simple, concise, English-like instructions easy to read even by
non-programmers.
• Object-Oriented Language: Interactive, interpreted, and Object-Oriented Programming language.
• Higher Productivity: Small codes + extensive libraries = more done with less writing compared to C++
and Java.
• Less Learning Time: Simple and shorter code means less time needed to learn Python.
• GUI Programming: Supports GUI applications for Windows MFC, Macintosh, and X Window (UNIX).
• Ample Availability of Libraries: Large standard libraries with numerous built-in modules.
• Syntax Highlighting: Distinguishes input, output, and error messages by different colour codes.

2.4 Limitations of Python


• Speed: Slower than C or C++ because it is a high-level language not closer to hardware.
• Mobile Development: Not considered good for mobile computing; very few mobile apps are built in it
— seen as a weak language for mobile.
• Memory Consumption: Not ideal for memory-intensive tasks; due to flexibility of data types, memory
consumption is high.
• Database Access: Its database access layer is underdeveloped and primitive compared to
JDBC/ODBC. Cannot be applied in enterprises needing smooth interaction with complex legacy data.
• Runtime Errors: Being dynamically typed, it requires more testing and has errors that only show up at
runtime.

2.5 Installing Python


• Several versions of Python have been released since Python 1.0.
• We work on Python 3.x (currently Python 3.12 is in use).
• Python is pre-installed on most Unix/Linux and macOS systems.
• Download Python from: [Link]
• The installation package includes: Python IDLE, Python Interpreter, and pip (Package Installer for
Python).

CTM: Python is used by Google Search Engine, YouTube, Netflix, Spotify, Dropbox, Instagram, etc. It
is widely used for System programming, GUI programming, Internet scripting, Web Gaming, Text
processing, Network programming, Commercial Robots, and Scientific applications.

2.6 Interacting with Python (Python IDLE)


Python is a flexible, dynamic, and powerful language that can be used in different ways — interactively to
test code line by line, or via script files.
• IDLE stands for Integrated Development and Learning Environment.
• IDLE allows the user to edit, run, browse, and debug a Python program from a single interface.
• Python IDLE comprises two modes:
– Python Shell — Interactive Mode
– Python Editor — Script Mode

2.6.1 Python Shell (Interactive Mode)


• Launch: Start → Apps by name → IDLE (Python 3.x)
• On opening, the shell shows a welcome message with version info and copyright notice.
• The command prompt (>>>) followed by a blinking cursor indicates IDLE is ready.
• Interactive window where you type Python code and see output in the same window.
• It is the interface between Python commands and the OS.
• When commands are entered directly from the keyboard, the interpreter is in Interactive Mode.
• Each statement is executed immediately after pressing Enter; output is displayed then and there.

CTM: The interactive interpreter of Python is termed as Python Shell.

2.6.2 Command Line Interaction


• In Interactive mode, type a command in front of the prompt (>>>) and press Enter.
• print() is the function used to display specified content on the screen.
• Content (called argument) is specified within parentheses.
• A print() statement without arguments simply jumps to the next line.
• Strings/messages must be placed within quotation marks (single ' ' or double " ").
• If argument is not in quotes, the interpreter tries to evaluate it as an expression.
• Python can also be used as a calculator — arithmetic expressions can be typed directly.

CTM: Python is a case-sensitive language — it treats upper- and lower-case characters differently. For
example, print() is a valid command but Print() will give a NameError.

CTM: To repeat a command in IDLE, press Alt+P to save time and typing effort.

Syntax of print() Function

CODE / SYNTAX

print(value, sep=' ', end='\n', file=[Link])

value : Values to be printed

sep : String inserted between values; default is a space

end : String appended after the last value; default is newline (\n)

file : Optional; by default, prints to the screen

Consecutive arguments are separated by commas. Space is the default separator between values in the
output. A different separator can be specified using the sep argument.

Statement Output

print(10, 20, 30) 10 20 30

print(10, 20, 30, sep='*') 10*20*30

print(10, 20, 30, sep='-*-') 10-*-20-*-30

print(10, 20, 30, sep=',') 10,20,30

print(10, 20, 30, sep='\n') 10\n20\n30 (each on new line)

print(10, 20, 30, sep='\t') 10 20 30 (tab-separated)

print('cow','cat','dog', sep=',', end='!!!\n') cow,cat,dog!!!

print(10 + 55) 65

print(22/7) 3.142857142857143

print(3.14*20.8*20.8) 1358.4896000000003

Note: If the sep argument is used, it must be specified after all the values to be printed, otherwise a
SyntaxError occurs (positional argument follows keyword argument).

Note: In Python command-line or interactive mode, an extra space between the prompt (>>>) and the
command will produce a syntax error.

2.6.3 Python Editor Window (Working in Script Mode)


• In Interactive Mode there is no provision for saving commands; all commands and output are cleared
when the Shell window is closed.
• Script Mode allows you to write, save, and run multi-line programs.
• A script is saved with the .py extension.
• Switch to Script Mode: File → New File (or Ctrl+N) in the Python Shell.
• The new window opens as 'Untitled' by default; rename it on saving.

Steps to Work in Script Mode

Step Action

Step 1 Open Python Shell → File → New File (Ctrl+N). A blank editor window opens.

Step 2 Type your Python statements/code in the blank file.

Step 3 Save the file: File → Save As (Ctrl+Shift+S). Give a name with .py extension (e.g.,
[Link]). Python IDLE auto-adds .py if forgotten.

Step 4 Run the program: Run → Run Module (F5). Output appears in the Python Shell window.

• In Script Mode, pressing Enter does NOT show output automatically — you must run the file (F5).
• Output from a script is always displayed inside the Python Shell window, not the editor.
• The Python interpreter takes the whole program, debugs it line-by-line, then produces output.
• Method of typing commands using print() statements is called Printing.

Python Shell Menu Items

Menu Purpose

File New File, Open, Recent Files, Save, Save As, Print Window, Close, Exit.

Edit Undo, Redo, Cut, Copy, Paste, Select All, Find and Replace.

Shell Restart the shell or search shell's log to trace the most recent reset.

Debug Debug Python programs; trace errors by line-by-line execution.

Options Configure IDLE — font style, highlighting colour, shortcut keys, etc.

Window Manage open windows.

Help Python help and documentation.

Interactive Mode vs Script Mode — Comparison

Feature Interactive Mode (Shell) Script Mode (Editor)

Prompt >>> (command prompt) No prompt; blank editor

Execution Line by line, immediately Whole program at once (F5)

Saving Cannot save commands Saved as .py file


Output Displayed in the same window Displayed in Python Shell

Use Testing, exploring, quick calc Writing complete programs

Mode entry Default on opening IDLE File → New File (Ctrl+N)

2.7 Exiting Python


• Press Ctrl+Q shortcut key, OR
• Type quit() or exit() and press Enter, OR
• Use shortcut key Alt+F4.
• If quit()/exit() do not work, type: import sys; [Link]()

Quick Summary: Salient Features of Python (Fig. 2.3)

✦ Easy ✦ Expressive ✦ Free and Open Source

✦ Portable ✦ High Level ✦ Object-Oriented

✦ Extensible ✦ Embeddable ✦ Interpreted

✦ Large Standard Library ✦ GUI Programming ✦ Dynamically Typed

Memory Bytes (Key Points to Remember)

■ POINT TO REMEMBER

✔ A complete set of instructions written using a programming language is termed as a Program / Code /
Program Code.

✔ Python is a powerful and flexible programming language using concise and easy-to-learn syntax.

✔ Python was developed by Guido van Rossum in 1991.

✔ Python is a platform-independent programming language.

✔ Python interpreter executes one statement (command) at a time.

✔ Python is an interpreted, interactive, directly executed language with a pre-compiled code.

✔ Python is a case-sensitive language — print() and Print() are different.

✔ Python is a loosely-typed language — you don't need to declare variable types.

✔ Python programs are typically 3 to 5 times shorter than equivalent C++ / Java programs.

✔ IDLE = Integrated Development and Learning Environment.

✔ Python Shell = Interactive Mode; Python Editor = Script Mode.

✔ Script files are saved with the .py extension.

✔ To run a script: Run → Run Module or press F5.


✔ To exit Python: Ctrl+Q or quit() or exit() or Alt+F4.

✔ print() function converts the argument into a string before writing to the screen.

✔ Default separator in print() is a space; default end is a newline (\n).

✔ Alt+P in IDLE repeats the previous command.

✔ Dynamically typed means variable types are determined at runtime — faster writing but more runtime
errors.

Brief History of Python

Version / Event Details

ABC Language Teaching language that inspired Python; created to replace BASIC.

Python 1.0 First official release; began the Python journey.

Python developed Guido van Rossum, 1991, National Research Institute, Netherlands.

Based on ABC language; developed using C language.

Inspiration Monty Python's Flying Circus (BBC comedy show).

Current usage Python 3.x (3.12 as of current class).

Used by Google, YouTube, Netflix, Spotify, Dropbox, Instagram and many more.

Practice / Class Exercises

Exercise 1 — Predict the Output

CODE / SYNTAX

a) num1 = 4

num2 = num1 + 1

num1 = 2

print(num1, num2)

b) print(8 + 5)

print(3.14 * 7)

print("I am a class XI" + "student")

Statement Output

print(num1, num2) [after num1=4, num2=5, num1=2] 25

print(8+5) 13

print(3.14*7) 21.98
print("I am a class XI"+"student") I am a class XIstudent

Exercise 2 — print() with sep and end

CODE / SYNTAX

print('Welcome to programming')

print(10, 20, 30, sep='@')

print('Python', 'Programming')

Statement Output

print('Welcome to programming') Welcome to programming

print(10, 20, 30, sep='@') 10@20@30

print('Python', 'Programming') Python Programming

Exercise 3 — Interactive Mode Tasks


• a) Display the sum of 3, 8, 6×12 → type: print(3 + 8 + 6*12)
• b) Display the sum of 16, 5.0, 44.0 → type: print(16 + 5.0 + 44.0)

Class XI • Informatics Practices • Chapter 2: Getting Started with Python

You might also like