Nothing Special   »   [go: up one dir, main page]

Chapter 2 Python Fundamentals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Chapter - 2

PYTHON FUNDAMENTALS
1. Define the character set in Python.
Ans)Character set is a set of valid characters that a language can recognize.
A character set represents any letter , digit, or any other symbol. Python has
the following character set:
Letters(A-Z , a-z) , Digits (0-9), Special symbols ( space, +, -, *,/ ,( ) etc ) ,
White spaces ( Blank space , tab, newline etc) , Other characters (Python can
process all ASCII and Unicode characters as part of data or literals.)
2. What is meant by Token? Name the Tokens available in Python.
Ans)The smallest individual unit in a program is known as a Token or a
lexical unit.
The Tokens available in Python are i)Keywords ii)Identifiers iii)Literals iv)
Operators v)Punctuators.
3. What is a keyword?Can keywords be used as identifiers.
Ans)A keyword is a word having special meaning reserved by programming
language.Eg.) if, elif, else etc.
No, key words cannot be used as identifiers.
4. What is an identifier ? What is the identifier forming rule of Python?
Ans)Identifier is the user defined name given to a part of a program like
variables, object, function etc.
Identifier forming rule:
 An identifier is an arbitrary long sequence of letters and digits.
 The first character must be a letter, the underscore( _ ) counts as a letter
 Upper and lower_case letters are different.All characters are significant.
Eg.)Myfile, DATE_7_77 , Z2T0Z9, _DS , _HJ13_JK , FILE13
5. Is Python case sensitive?What is meant by the term ‘case sensitive’?
Ans)Yes, Python is case sensitive as it treats upper and lower case characters
differently.
6. Which of the following are valid identifiers and why/ why not:
Ans)i)Data_rec (valid) ii)_data (valid) iii) 1 data (invalid because it is
starting with digit) iv)data1 (valid) v)my.file (invalid because it contains
special character dot( .) vi)elif ( reserved keyword) vii)switch (reserved
word) viii)lambda ( reserved keyword) ix)break ( reserved keyword).
7. What are literals ? How many types of literals are available in C++?
Ans)Literals are data items that have a fixed value. The literals available in
Python are i)String literals ii)Numeric literals iii)Boolean literals iv)Special
literal None iv)Literal collections.
8. What are string literals in Python?
Ans) The text enclosed in single quotes or double quotes forms a string
literal.Eg) “a” , „x‟ , “abc”, „xyz‟ , “129045”, „1-x-0-w-25‟, “Amy‟s”
9. What are non graphic characters?
Ans)Nongraphic characters are those characters that cannot be typed
directly from keyboard. Eg.) backspace ,tabs, carriage return etc.
(No characters is typed when these keys are pressed , only some action takes
place)
10.How are non graphic characters represented in C++?
Ans)Non graphic characters can be represented by using escape
sequences.An escape sequence is represented by a backslash (\) followed by
one or more characters.eg) \n (new line character) , \t (horizontal tab ),\v
(vertical tab) etc.
11.An escape sequence represents how many characters?
Ans)An escape sequence represents a single character and hence consumes
one byte in ASCII representation.
12.Why are characters \ , ‘ , “ and tab typed using escape sequence?
Ans)If these characters are to be typed as it is , use escape sequence , \\ (
backslash ) , \‟ ( Single quote ), \” ( Double quote). With out escape
sequence ,these carry a special meaning and have a special purpose.
13.How many ways , can you create String literals in Python?Explain.
Ans) Two ways
i. Single line Strings : Strings enclosed in single quotes („ „) or double
quotes (“ “) and are terminated in single line.
Eg) Text = „hello' or Text = “hello”
ii. Multiline Strings : Strings storing multiple lines of text.
14.How can you create multi-line strings in Python?
Ans)Multiline strings can be created in two ways:
i. By adding a backslash at the end of normal single-quote or double –
quote strings. Eg) Text = “Welcome\ or Text =‟Welcome\
To\ To\
Python” Python‟
ii. By typing the text in triple quotation marks( 3 single quotes or 3
double quotes).(No backslash needed at the end of line).
Eg) Text = „ „ „ Welcome
To
Python „ „ „
or
Text = “ “ “ Welcome
To
Python “ “ “
15.How to calculate the size of a string in Python? What will be the size of
the following string constant?
Ans)The size of string is calculated by counting the number of characters in
the string. If string literal has an escape sequence contained within it,count
the escape sequence as one character
String Size String Size
„\\‟ 1 “Reema\‟s” 7
„abc‟ 3 „\”‟ 1
“\ab” 2 “it‟s” 4
“Seema\‟s pen” 11 “xy\ 4
yz”
“Amy‟s” 4 “ “ “ xy 5 (Triple quoted multi
yz “ “ “ line string , EOL
character is also
counted in size)
„\a‟ 1
“\a” 1
Note: Python allows single quote without escape sequence in a double
quoted string and double quote without escape sequence in a single quoted
string.
16.What are the numeric Literals in Python?
Ans)The Numeric Literals are 1.) Integer Literals 2.)Floating point Literals
17.What is an Integer Literal?
Ans)Integer literals are whole numbers without any fractional part.An
integer constant must have at least one digit and must not contain any
decimal point.It may contain either (+) or ( -) sign. A number with no sign
is assumed to be positive. Commas cannot appear in an integer constant.
18.How many types of integer literals are allowed in C++?Explain.
Ans)Python allows three types of integer literals.
a)Decimal integer literals (base 10 ) – An integer literal consisting of a
sequence of digits is taken to be decimal integer literal unless it begins with
0. Eg) 1234, +97, -174
b)Octal integer literal ( base 8) - A sequence of digits starting with 0 is
taken to be an octal integer. Eg) (8)10 =010 , (12)10=014
c)Hexadecimal integer literal ( base 16) – A sequence of digits preceded by
0x or 0X is taken to be an hexadecimal integer. Eg)(12)10= 0XC,
19.What is the difference between 25L and 25?
Ans)An l or L suffix indicates it is long integer constant. Thus 25L is a long
integer value and 25 is an integer value.
20.What is meant by a floating – point literal in Python? How many ways
can a floating literal be represented into?
Ans)Floating literals are also called real literals. Real literals are numbers
having fractional parts. It can be represented in
i) Fractional
ii) Form: A real constant in fractional form must have at least one digit
before a decimal point and at least one digit after the decimal point. It
may also have either + or – sign preceding it. A real constant with no
sign is assumed to be positive.Eg) 2.0, 17.5, -13.0,-0.00625
iii) Exponent form : A real constant in exponent form has two parts: a
mantissa and an exponent. The mantissa must be either an integer or a
proper real constant. The mantissa is followed by a letter E or e and
the exponent. The exponent must be an integer. Eg) 152E05,
-0.172E-3 ,
21.What are the two Boolean literals in Python?
Ans)True and False are the only two Boolean literal values in Python.
22.What is None literal in Python?
Ans)Python has one special literal called None. The None literal is used to
indicate something that has not yet been created in simple words, or absence
of value. It is also used to indicate the end of lists in Python.
23.What are literal collections?
Ans)tuples and lists are the literal collections in Python.
24.Name some built – in literals of Python.
Ans)Boolean literals True , False and special literal None are some built-in
constants/literals of Python.
25.What are Operators in Python?
Ans)Operators are tokens that trigger some computation / action when
applied to variables and other objects in an expression.Eg. Unary Operators,
Binary Operators
26.What are Punctuators?
Ans)Punctuators are symbols that are used in programming languages to
organize programming- sentence structures and indicate the rhythm and
emphasis of expressions , statements and program structure.
27.What all components can a Python program contain?
Ans)Expressions, Statements, Comments, Function, Blocks and indentation
are the various components of a Python program.
28.What is an expression in Python?
Ans)An expression is any legal combination of symbols that represents a
value.Eg) 15, a+10 , b>5 etc
29.What is a statement in Python ?
Ans)A statement is a programming instruction that does something. A
statement executes and may or may not yield a value.
Eg . of statements) a= 15
b= a -10
print(a+3)
30.What is the difference between an expression and a statement in
Python?
Ans)
Expression Statement
Legal combination of symbols Programming instruction as per
Python syntax
Represents something Does something
Python evaluates it Python executes it
End result is a value Need not result in a value
Eg)(3+5)/4 Eg)Print(“Hello”)
If a> 0 :

31.What is a comment ? What are the different ways to create comments in


Python?
Ans)Comments are the additional readable information to clarify the source
code.
i.)Full line comments – The physical lines beginning with # are the full line
comments.
Eg) #This program shows a program‟s component
#Definition of function SeeYou() follows
#Main program code follows now
ii.)Inline comment – It starts in the middle of a physical line, after Python
code. Eg) if b<5: # colon means it requires a block
iii.)Multi line comments ( block comment) – It can be done in two ways
a)Add a # symbol in the beginning of every physical line part of the
multi – line comments.
Eg.) #Multi-line comments are useful for detailed description
#Related to the program in question
#It helps clarify certain important things.
b)Type comment as a triple quoted (“ “ “ ) or triple apostrophe („ „ „) multi
– line string.
Eg) „ „ „ Multi-line comments are useful for detailed description
Related to the program in question
It helps clarify certain important things.
„„„
32.What is docstring? What is the use of docstrings?
Ans)Multiline comments enclosed in triple quotes (“ “ “) or triple apostrophe
(„ „ „) are called docstrings. The docstrings are very useful in documentation.
33.What is a function?
Ans)A function is a code that has a name and it can be reused by specifying
its name in the program, where needed.
34.What is a block or suite in Python? How is indentation related to it?
Ans)A group of statements which are part of another statement or a function
are called block or code-block or suite in Python.
Python uses indentation to create blocks of code. Statements at same
indentation level are part of same block/suite. Statements requiring suite/
code- block have a colon (:) at their end.
35.What is a variable?
Ans)A variable in Python represents named location that refers to a value
and whose values can be used and processed during program run.
36.Why is a variable called symbolic variable?
Ans)The variables are called symbolic variables because these are named
labels.
37.Create the variables for the following
i)to hold a train number
ii)to hold the name of a subject
iii)to hold balance amount in a bank account
iv)to hold a phone number
Ans) i)TrainNo = „T#1234‟
ii)Subject = „Maths‟
iii)Balance =23456.75
iv)PhoneNumber = 0549648141
38.What is lvalue and rvalue?
Ans)lvalue : Expressions that can come on the left hand side of an
assignment.
rvalue : Expressions that can come on the right hand side of an assignment.
39.Explain multiple assignments in Python.
Ans) i)Assigning same value to multiple variables
Assign same value to multiple variables in a single statement.
Eg) a = b = c = 10 ( It will assign same value 10 to all 3 variables a, b, c.)
ii)Assigning multiple values to multiple variables
Assign multiple values to multiple variables in single statement.
Eg) x, y, z = 10, 20, 30
x=10, y= 20, z= 30
40.What is the output?
i) a, b, c = 5, 10, 7 ii) p , q = 3 , 5
b, c, a = a+1 , b+2, c-1 q , r = p-2, p+2
print(a, b, c) print(p,q,r)
Ans)6 6 12 Ans)3 1 5

iii) x = 10 iv) x , x = 20, 30


y,y = x+2 , x+5 y , y = x + 10, x + 20
print(y) print(x , y)
Ans)15 Ans)30 50
41.What is Name Error?
Ans)A variable is defined only when you assign some value to it. Using an
undefined variable in an expression/ statement causes an error called Name
Error.
Eg) print(x) # error name „x‟ not defined
x = 20
Print(x)
42.What happens when you try to access the value of an undefined
variable?
Ans)It will generate Name Error.
43.What do you understand by undefined variable in Python?
Ans)A variable is not created until some value is assigned to it.
Eg) print(x) # „x‟ is not defined
x = 20
Print(x)
44.What is Dynamic Typing? What is the caution you must take care of ?
Ans) A variable pointing to a value of a certain type, can be made to point
to a value of different type. This is called Dynamic Typing.
Eg1) x = 10
print(x)
x = “Hello World”
print(x)
Eg2) x= 10
y=0
y = x/2
x=‟Day‟
y = x/2 #ERROR!! A string cannot be divided
45.How to determine the type of a variable?
Ans)type(<variable name >)
Eg) a= 10
type(a) # The type returned is int (integer)
b = „hello‟
type(b) # The type returned is str (string)
46.How to input and output values in Python?
Ans)input( ) – the built in function input() is used to get input from user
interactively.
Syntax: variable_ to_hold_the_value = input(<prompt to be displayed>)
Note: The input() function always returns a value of String type.
Eg)>>> name = input(„What is your name?‟)
What is your name? Simar
>>>age = input(„Enter your age:‟)
Enter your age: 16
47.What is wrong with the following statement?
Number = input(“Number”)
Sqr = Number * Number
Ans)input() function returns the value of Number as string type.So
arithmetic calculation cannot be performed.
48.What is Type Error?
Ans)When you try to perform an operation on a data type not suitable for it (
Eg, dividing or multiplying a string), Python raises an error called
TypeError.
49.How to get input from user interactively?
Ans)input( )
Syntax : variable _to_hold_the_value = input(<prompt to be displayed>)
eg)name = input(„What is your name ? „ )
age = input(„Enter your age‟)
Note : The input( ) function always returns a value of String type.
50.What is TypeError?
Ans)When you try to perform an operation on a data type not suitable for it
(eg, dividing or multiplying a string ) , python raises an error called
TypeError.
51.How to convert the values received through input( ) into int and float
types?
Ans)Function int( ) around input( ) converts the read value into int type and
function float( ) around input ( ) function converts the read value into float
type.
Eg) age = int(input(„Enter your age‟))
marks = float(input(„Enter marks : „))
Note: age like 17 is int compatible
age like 17.5 , Seventeen is not int compatible.
marks like .73 or 73 or 73 or 73.5 etc are easily converted into float.
Marks like 73.5.0, 73 percent are not compatible into float.
52.How to output values to standard output device?
Ans)The print( ) function send output to standard output device monitor.
Eg) i) print(„Python is wonderful.„) the output is Python is wonderful
ii)print(„Sum of 2 and 3 is „ , 2 + 3) the output is Sum of 2 and 3 is 5
iii)a=25
print(„Double of „ , a , „is‟ , a*2) the output is Double of 25 is 50
iv)print( ) – it prints blank line.
53.What are the features of print statement?
Ans)* It automatically - converts the item into equivalent string and print it.
*It inserts spaces between items automatically (because the default
value of sep argument is space („ „).(sep argument specifies the separator
character.)
Eg)print(“My”,”name”,”is”,”Amit.”) output is My name is Amit
print(“My”,”name”,”is”,”Amit.”,sep =”…”)
output is My…name…is…Amit.
*It appends a newline character at the end of the line unless it is given.
Eg)print(„My name is Amit.‟)
Print(„I am 16 years old‟)
Output is
My name is Amit.
I am 16 years old
*end argument in print( ) will print the line and end it with the string
specified with the end argument
Eg)print(„My name is Amit.‟ , end = „$‟)
Print(„I am 16 years old.‟)
Output is My name is Amit. $I am 16 years old.
Eg) a, b = 20, 30
Print( “a=” , a,end=‟ „)
Print(“b=”,b)
Output is
a=20 b=30
Note: Since there is end character given as space (end = „ „) in first print
statement , the new line („\n) character is not appended at the end of
output generated by first print statement.Thus the cursor stays on the
same line. Hence the output of second print statement appears in the
same line.
Eg) Name = „Enthusiast‟
Print(“Hello”, end = „ „)
Print(Name)
Print(“How do you find Python?”)
Output is
Hello Enthusiast
How do you find Python?

_______________________________

You might also like