O Level Exam

Numbers in Python

Number data type stores Numerical Values. This data type is immutable i.e. value of its object cannot be changed (we will talk about this aspect later). These are of three different types:

a) Integer & Long

b) Float/floating point

c) Complex

Range of an integer in Python can be from -2147483648 to 2147483647, and long integer has unlimited range subject to available memory.

1.1 Integers are the whole numbers consisting of + or – sign with decimal digits like 100000, -99, 0, 17. While writing a large integer value, don‟t use commas to separate digits. Also integers should not have leading zeros.

When we are working with integers, we need not to worry about the size of integer as a very big integer value is automatically handled by Python. When we want a value to be treated as very long integer value append L to the value. Such values are treated as long integers by python.

`>>> a = 10>>> b = 5192L #example of supplying a very long value to a variable>>> c= 4298114>>> type(c) # type ( ) is used to check data type of value<type 'int'>>>> c = c * 5669>>> type(c)<type 'long'>`

Integers contain Boolean Type which is a unique data type, consisting of two constants, True & False. A Boolean True value is Non-Zero, Non-Null and Non-empty.

Example

`>>> flag = True>>> type(flag)<type 'bool'>`

1.2 Floating Point: Numbers with fractions or decimal point are called floating point numbers.

A floating point number will consist of sign (+,-) sequence of decimals digits and a dot such as 0.0, -21.9, 0.98333328, 15.2963. These numbers can also be used to represent a number in engineering/ scientific notation.

-2.0X 105 will be represented as -2.0e5

2.0X10-5 will be 2.0E-5

Example

y= 12.36

A value when stored as floating point in Python will have 53 bits of precision.

1.3 Complex: Complex number in python is made up of two floating point values, one each for real and imaginary part. For accessing different parts of variable (object) x; we will use x.real and x.image. Imaginary part of the number is represented by „j‟ instead of „i‟, so 1+0j denotes zero imaginary part.

Example

`>>> x = 1+0j>>> print x.real,x.imag1.0 0.0`

Example

`>>> y = 9-5j>>> print y.real, y.imag9.0 -5.0`

Qus. When was Python released?

Ans- Python was released in February 1991.

Qus. Who developed Python?

Ans- Python was developed by Guido Van Rossum.

Qus. Which two languages contributed to Python as a Programming Language?

Ans- ABC language and Modula 3.

Qus. Is Python an Object Oriented Language?

Ans- Yes, Python is an object oriented language.

Qus. Python got its name from which show?

Ans- Python got its name from famous BBC comedy show “Monty Python’s Flying Circus”.

Qus. "Python is an interpreted language". What does it mean to you?

Ans- It means that the Python installation interprets and executes the code line by line at a time.

Qus. What does a cross platform language mean?

Ans- it means a language can run equally on variety of platforms-Windows, Linux/UNIX, Macintosh, Supercomputers, Smart phones etc.

Qus. Python is a Free and Open Source language. What do you understand by this feature?

Ans- It means Python is freely available without any cost. Its source code is also available. One can modify, improve/extend an open source software.

Qus. In how many ways, can you work in Python?

Ans-In two ways we can work in Python-

(i) Interactive mode

(ii) Script Mode

Qus. What is the difference between Interactive mode and Script Mode in Python?

Ans- In interactive mode, one command can run at a time and commands are not saved. Whereas in Script mode, we can save all the commands in the form of a program file and can see output of all lines together.

Qus. What are the advantages of Python?

i. Easy to use OO Language.

ii. Expressive Language.

iii. Interpreted Language.

iv. It is complete.

v. Cross-Platform Language.

vi. Free and Open Source.

vii. Variety of Usage/ Applications

Qus. What are the limitations of Python?

Ans- limitations-

i. Not the fastest language.

ii. Lesser Libraries than C, Java, Perl.

iii. Not strong on Type Binding.

iv. Not easily convertible.

Qus. Which of the following are not valid strings in Python?

(a)‖Hello‖ (b) ‗Hello‘ (c)‖Hello‘ (d) ‗Hello‖ (e) {Hello}

Ans- String (c) , (d) and (e ) are not valid strings.

Qus. Write Instructions in python to get the following result: (Do it in both interactive mode and script mode)

I am a student of KV Barabanki

I live in Barabanki

And I love Barabanki.

Barabanki is 20 KM away from Lucknow

This Place is famous for Dewa Sharif

Ans- In Interactive Mode-

>>> print("I am a student of KV Barabanki \nI live in Barabanki \nAnd I love Barabanki\n\n Barabanki is 20 KM away from Lucknow\n This Place is famous for Dewa Sharif")

In Script Mode-

print("I am a student of KV Barabanki \n I live in Barabanki \nAnd I love Barabanki\n\n Barabanki is 20 KM away from Lucknow\n This Place is famous for Dewa Sharif")

(save this code to a file and then run the file)

Qus. What is None literal in Python?

Ans: Python has one special literal, which is None. The None literal is used to indicate absence of value. It is also used to indicate the end of lists in Python. It means ―There is nothing here‖.

Qus. What is the difference between a keyword and an identifier?

Ans: Difference between Keyword and Identifier: Every language has keywords and identifiers, which are only understood by its compiler. Keywords are predefined reserved words, which possess special meaning. An identifier is a unique name given to a particular variable, function or label of class in the program.

Qus. What are literals in Python? How many types of Literals allowed in Python?

Ans: Literals: Python comes with some built-in objects. Some are used so often that Python has a quick way to make these objects, called literals.

The literals include the string, Unicode string, integer, float, long, list, tuple and dictionary types.

Qus. How many types of sequences are supported in Python?

Ans: Three Types of Sequences are supported in python:

(i) String

(ii) List

(iii) Tuple

Qus. What factors guide the choice of identifiers in program?

Ans:

(i) An identifier must start with a letter or underscore followed by any number of digits and/or letters.

(ii) No reserved word or standard identifier should be used.

(iii) No special character (Other than underscore) should be included in the identifier.

Qus. What is the difference between an expression and a statement in Python?

Ans: A statement is an instruction that the Python interpreter can execute. We have only seen the assignment statement so far. Some other kinds of statements that we‘ll see shortly are while statements, forstatements, if statements, and import statements. (There are other kinds too!) An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated. If you ask Python to print an expression, the interpreter evaluates the expression and displays the result.

Qus. What are tokens in Python? How many types of tokens allowed in Python?

Ans: Tokens are the smallest unit of the program. There are following tokens in Python:

•  Reserved words or Keywords
•  Identifiers
•  Literals Definition of all tokens may come. Which is not given
•  Operators in this question bank.
•  Punctuators

Qus. What are data types? What are Python‟s built-in core data types?

Ans. Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. There are various data types in Python. Some of the important types are listed below.

(i) Numbers (ii) String (iii) List (iv) Tuple (v) Dictionary

Qus. Which data types of Python handle Numbers?

Ans. It is cleared by name that Number data types are used to store numeric value in Python. The Numbers in Python have following core data types:

(i) Integers

a. Integers (signed)

b. Booleans

(ii) Floating-Point Numbers

(iii) Complex Numbers

Qus. Why is Boolean considered a subtype of Integers?

Ans: Because Boolean Values False and True behave like the values 0 and 1, respectively. So Boolean type is a subtype of plain integers.

Qus. What do you understand by term "immutable"?

Ans: Immutable types are those data types that can never change their value in place. In Python the following types are immutable:

(i) integers

(ii) floating-point numbers

(iii) Booleans

(iv) Strings

(v) Tuples

Qus. What are mutable and immutable types in Python? List both of them.

Ans. Mutable types means those data types whose values can be changed at the time of execution.

They are as follows:

• Lists
• Dictionaries
• Sets

Immutable types are those data types that can never change their value in place. In Python the following types are immutable:

• integers
• floating-point numbers
• Booleans
• Strings
• Tuples

Programs

Write a python program to input a welcome message and print it

Write a python program that calculate and prints the number of seconds in a year.

Write a program that inputs a string and then prints it equal to number of times its length, e.g.,

Enter string : "eka"

Result ekaekaeka