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

Python Basic

Uploaded by

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

Python Basic

Uploaded by

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

Chapter-5

Getting Started with Python


Q-1 What is Program in Computer Language?
Ans.- An ordered set of instructions to be executed by a
computer to carry out a specific task is called a program.
Q-2 What is Programming Language?
Ans. - The language used to specify this set of instructions to the
computer is called a programming language.
Q-3 What is Source Code?
Ans.- A Computers understand the language of 0s and 1s which is
called machine language or low-level language. So, it is difficult
for human to write instructions using 0s and 1s. So user need
high-level programming languages like Python, C++, that are
easier to manage by humans but are not directly understood by
the computer.
A program written in a high-level language is called source code.
Q-4 What is the role of Translator in programming Language?
Ans.- Language translators like compilers and interpreters are
used to translate the source code into machine language. Python
uses an interpreter to convert its instructions into machine
language, so that it can be understood by the computer.
An interpreter processes the program statements one by
one, first translating and then executing. This process is
continued until an error is found or the whole program is
executed successfully. In both the cases, program execution will
stop.
A compiler translates the entire source code, as a whole,
into the object code. After scanning the whole program, it
generates error message if found any.
Q-5 Write the features of Python.
Ans.- Python is a high-level language. It is a free and open source
language.
• It is an interpreted language, as Python programs are executed
by an interpreter.
• Python programs are easy to understand as they have a clearly
defined syntax and relatively simple structure.
• Python is case-sensitive. For example, NUMBER and number
are not same in Python.
• Python is portable and platform independent, means it can run
on various operating systems and hardware platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many popular web
services and applications are built using Python.
• Python uses indentation for blocks and nested blocks.
Q-6 How can a user write and run the Program?
Ans. To write and run (execute) a Python program, we need to
have a Python interpreter installed on our computer or we can
use any online Python interpreter. The interpreter is also called
Python shell.
In Python Shell, the symbol >>> is the Python prompt, which
indicates that the interpreter is ready to take instructions. We
can type commands or statements on this prompt to execute
them using a Python interpreter.
Q-7 What are Execution mode in Python?
Ans.- There are two ways to use the Python interpreter:
(A) Interactive Mode - To work in the interactive mode, we
can simply type a Python statement on the >>> prompt
directly. As we press enter, the interpreter executes the
statement. 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.

Q-8 How a Program is saved in Python?


Ans. - 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 prog1.py, we type
prog1.py.
b) While working in the script mode, after saving the
file, click [Run]-> [Run Module] from the menu.
c) The output appears on shell.
Q-9 What are Python Keywords?
Ans.- Keywords are reserved words. Each keyword has a specific
meaning to the Python interpreter, and we can use a keyword in
our program only for the purpose for which it has been defined.
As Python is case sensitive, keywords must be written exactly as
given in Table below.

Q-10 What are Identifiers?


Ans.- identifiers are names used to identify a variable, function,
or other entities in a program. The rules for naming an identifier
in Python are as follows:
• The name should begin with an uppercase or a lowercase
alphabet or an underscore sign (_). This may be followed by any
combination of characters a–z, A–Z, 0–9 or underscore (_). Thus,
an identifier cannot start with a digit.
• It can be of any length. (However, it is preferred to keep it short
and meaningful).
• It should not be a keyword or reserved word given
• We cannot use special symbols like !, @, #, $, %, etc., in
identifiers.
Q-11 What is Variable in a Prog. Language?
Ans. A variable in a program is uniquely identified by a name
(identifier). Variable in Python refers to an object — an item or
element that is stored in the memory. Value of a variable can be
a string, numeric or any combination of alphanumeric characters
(CD67). In Python we can use an assignment statement to create
new variables and assign specific values to them.
message = "Keep Smiling"
Variable declaration is implicit in Python, means variables are
automatically declared and defined when they are assigned a
value the first time. Variables must always be assigned values
before they are used in expressions as otherwise it will lead to an
error in the program.
Q-12 What does mean by ‘Comment’? How can we write
comment in Python?
Ans. Comments are used to add a remark or a note in the source
code. Comments are not executed by interpreter. They are added
with the purpose of making the source code easier for humans to
understand.
In Python, a comment starts with # (hash sign). Everything
following the # till the end of that line is treated as a comment.
Q-13 What does mean by ‘Everything is an Object’?
Ans. Python treats every value or data item whether numeric,
string, or other type (discussed in the next section) as an object in
the sense that it can be assigned to some variable or can be
passed to a function as an argument.
Every object in Python is assigned a unique identity (ID)
which remains the same for the lifetime of that object. This ID is
akin to the memory address of the object. The function id()
returns the identity of an object.
Q-14 What is Data type in Prog. Language?
Ans. Data type identifies the type of data values a variable can
hold and the operations that can be performed on that data.

Q-15 Define each Data type?


Ans. Numbers- Number data type stores numerical values only.
Variables of simple data types like integers, float, Boolean, etc.,
hold single values.
Sequence -A Python sequence is an ordered collection of items,
where each item is indexed by an integer. The three types of
sequence data types available in Python are Strings, Lists and
Tuples. We will learn about each of them in detail in later
chapters. A brief introduction to these data types is as follows:
(A) String -String is a group of characters. These characters
may be alphabets, digits or special characters including
spaces. String values are enclosed either in single
quotation marks (e.g., ‘Hello’) or in double quotation
marks (e.g., “Hello”). The quotes are not a part of the
string, they are used to mark the beginning and end of the
string for the interpreter.
(B) List - List is a sequence of items separated by commas
and the items are enclosed in square brackets [ ].
(C) Tuple - Tuple is a sequence of items separated by commas
and items are enclosed in parenthesis ( ). This is unlike list,
where values are enclosed in brackets [ ]. Once created,
we cannot change the tuple.
SET - Set is an unordered collection of items separated by
commas and the items are enclosed in curly brackets { }. A set
is similar to list, except that it cannot have duplicate entries.
Once created, elements of a set cannot be changed.
None- None is a special data type with a single value. It is used
to signify the absence of value in a situation. None supports no
special operations, and it is neither False nor 0 (zero).
Mapping -Mapping is an unordered data type in Python.
Currently, there is only one standard mapping data type in
Python called dictionary.
(A) Dictionary - Dictionary in Python holds data items in key-
value pairs. Items in a dictionary are enclosed in curly brackets
{ }. Dictionaries permit faster access to data. Every key is
separated from its value using a colon (:) sign. The key : value
pairs of a dictionary can be accessed using the key. The keys
are usually strings and their values can be any data type. In
order to access any value in the dictionary, we have to specify
its key in square brackets [ ].

You might also like