Getting Started Python Chapter 5 NEW 14-09-2024
Getting Started Python Chapter 5 NEW 14-09-2024
Getting Started Python Chapter 5 NEW 14-09-2024
Features of Python:
High-Level Language: Python is a high-level programming language that is free and
open-source.
Interpreted Language: Python programs are executed by an interpreter, making it an
interpreted language.
Readability: Python programs are easy to understand due to their clearly defined
syntax and relatively simple structure.
Case Sensitivity: Python is case-sensitive, meaning that `NUMBER` and `number` are
treated as distinct identifiers.
Portability: Python is portable and platform-independent, capable of running on
various operating systems and hardware platforms.
Rich Standard Library: Python includes a rich library of predefined functions.
Web Development: Python is useful in web development, with many popular web
services and applications built using the language.
Indentation: Python uses indentation to define blocks and nested blocks, ensuring a
clean and readable code structure.
Easy to use: It is easy to learn and use
Working in the interactive mode is convenient for testing a single line code for instant
execution. But in the interactive mode, we cannot save the statements for future use and we
have to retype the statements to run them again.
(B) Script Mode In the script mode, we can write a Python program in a file, save it and then
use the interpreter to execute it. Python scripts are saved as files where file name has
extension “.py”. By default, the Python scripts are saved in the Python installation folder. To
execute a script, we can either:
a) Type the file name along with the path at the prompt. For example, if the name of the file
is prog5-1.py, we type prog5-1.py. We can otherwise open the program directly from IDLE
as shown in Figure 3.
b) While working in the script mode, after saving the file, click [Run]->[Run Module] from
the menu as shown in Figure 4
c) The output appears on shell as shown in Figure 5
IDENTIFIERS
In programming languages, identifiers are names used to identify a variable, function, or
other entities in a program.
VARIABLES:
In a program, a variable is uniquely identified by its name, known as an identifier. In Python,
a variable refers to an object, which is an item or element stored in memory. The value of a
variable can be a string, a number, or any combination of alphanumeric characters.
We can create new variables and assign specific values to them using assignment statements
in Python.
gender = 'M'
message = "Have A Nice Day
regno = 111
Program : Write a program to display values of variables in Python.
COMMENTS
Comments are used to add remarks or notes in the source code. They are not executed by the
interpreter.
Comments are added to make the source code easier for humans to understand. They
primarily document the meaning and purpose of the code, including its input and output
requirements. This helps us remember how the code functions and how to use it later.
In large and complex software projects, multiple programmers often work in teams, and a
program written by one programmer may need to be used or maintained by another.
In such situations, comments are essential for understanding the workings of the program.
In Python, a comment starts with a # (hash sign). Everything following the # until the end of
that line is treated as a comment, and the interpreter ignores it while executing the code.
Example
#Variable amount is the total spending on
#Books
amount = 1500
Everything is an Object
In Python, every value or data item, whether numeric, string, or another type (discussed in the
next section), is treated as an object. This means it can be assigned to a variable or passed to a
function as an argument. Each object in Python is given a unique identity (ID) that remains
constant for the object's lifetime. This ID is similar to the memory address of the object. The
id() function returns the identity of an object.
Example 5.2
>>> num1 = 30
>>> id(num1)
1433920576 #identity of num1
>>> num2 = 50 - 10
>>> id(num2)
1433920576 #identity of num2 and num1
#are same as both
refers to #object 40