0% found this document useful (0 votes)
9 views7 pages

Python Data Types and Expressions Guide

This document covers fundamental concepts of Python programming, including definitions of interpreter, variables, data types, operators, and functions. It explains the differences between statements and expressions, the use of comments, and the importance of modular programming. Additionally, it provides examples of Python syntax and operations, such as floor division, function calls, and logical operators.

Uploaded by

renuka
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)
9 views7 pages

Python Data Types and Expressions Guide

This document covers fundamental concepts of Python programming, including definitions of interpreter, variables, data types, operators, and functions. It explains the differences between statements and expressions, the use of comments, and the importance of modular programming. Additionally, it provides examples of Python syntax and operations, such as floor division, function calls, and logical operators.

Uploaded by

renuka
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

UNIT II- DATA TYPES, EXPRESSIONS, STATEMENTS

2 mark

[Link] is meant by interpreter?

An interpreter is a computer program that executes instructions written in a programming language. It can either
execute the source code directly or translate the source code in a first step into a more efficient representation
and executes this code.

2. How will you invoke the python interactive interpreter?

The Python interpreter can be invoked by typing the command "python" without any parameter followed by the
"return" key at the shell prompt.

3. What are the commands that are used to exit from the python interpreter in UNIX and windows?

CTRL+D is used to exit from the python interpreter in UNIX and CTRL+Z is used to exit from the python
interpreter in windows.

4. Define a variable and write down the rules for naming a variable.

A name that refers to a value is a variable . Variable names can be arbitrarily long. They can contain both letters
and numbers, but they have to begin with a letter. It is legal to use uppercase letters, but it is good tobegin
variable names with a lowercase letter.

5. Write a snippet to display “Hello World” in python interpreter.

In script mode:

>>>print("Hello World")

Hello World

In Interactive Mode:

>>> "Hello World"

'Hello World'

6. List down the basic data types in Python.

Numbers

String

List

Tuple

Dictionary

7. Define keyword and enumerate some of the keywords in Python. (Jan-2019)

A keyword is a reserved word that is used by the compiler to parse a program. Keywords cannot be used as
variable names. Some of the keywords used in python are:

and
del

from

not

while

is

continue

8. What do you mean by an operand and an operator? Illustrate your answer with relevant example.

An operator is a symbol that specifies an operation to be performed on the operands. The data items that an
operator acts upon are called operands. The operators +, -,*, / and ** perform addition,
subtraction,multiplication, division and exponentiation.

Example: 20+[Link] this example, 20 and 32 are operands and + is an operator.

9. Explain the concept of floor division.

The operation that divides two numbers and chops off the fraction part is known as floor division.

Example:>>> 5//2= 2

10. Define an expression with example.

An expression is a combination of values, variables, and operators. An expression is evaluated using

assignment operator. Example:Y= X + 17

11. Define statement and mention the difference between statement and an expression.

A statement is a unit of code that the Python interpreter can execute. The important difference is that an
expression has a value but a statement does not have a value.

12. What is meant by rule of precedence? Give the order of precedence.

The set of rules that govern the order in which expressions involving multiple operators and operands are
evaluated is known as rule of precedence. Parentheses have the highest precedence followed by exponentiation.
Multiplication and division have the next highest precedence followed by addition and subtraction.

13. Illustrate the use of * and + operators in string with example.

The * operator performs repetition on strings and the + operator performs concatenation on strings.

Example:>>> ‘Hello*3’

Output:HelloHelloHello

>>>’Hello+World’

Output:HelloWorld

14. What is function call?


A function is a named sequence of statements that performs a computation. When you define a function, you
specify the name and the sequence of statements. Later, you can “call” the function by name is called function
call.

Example:

sum() //sum is the function name

15. What is a local variable?

A variable defined inside a function. A local variable can only be used inside its function.

Example:

deff():

s=

"Me too.

" // local variable

print(s)

a=

"I hate spam.

"

f()

print (a)

16. Define arguments and parameter.

A value provided to a function when the function is called. This value is assigned to the corresponding
parameter in the function. Inside the function, the arguments are assigned to variables called parameters.

17. What do you mean by flow of execution?

In order to ensure that a function is defined before its first use, you have to know the order in which
statements are executed, which is called the flow of execution.

Execution always begins at the first statement of the program. Statements are executed one at a time, in order
from top to bottom.

18. What is the use of parentheses?

Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you
want. It also makes an expression easier to read.
Example: 2 + (3*4) * 7

19. What do you meant by an assignment statement?

An assignment statement creates new variables and gives them values:

>>> Message ='And now for something completely different'>>> n = 17

This example makestwo assignments. The first assigns a string to a new variable namedMessage; the second
gives the integer 17 to n.

20. What is tuple? (or) What is a tuple? How literals of type tuples are written?

A tuple is a sequence of immutable Python objects. Tuples are sequences, like lists. The differences

between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists
use square brackets. Creating a tuple is assimple as putting different comma-separated values. Comma-separated
values between parentheses can also be used.

Example: tup1 = ('physics'

'chemistry'

, 1997, 2000); tup2= ();

21. Define module.

A module allows to logically organizing the Python code. Grouping related code into a module

makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that
can bind and reference.A module is a file consisting of Python code. A module can define functions,classes and
variables. A module can also include runnable code.

22. Name the four types of scalar objects Python has. (Jan-2018)

int

float

bool

None

23. List down the different types of operator.

Python language supports the following types of operators:

Arithmetic operator

Relational operator
Assignment operator

Logical operator

Bitwise operator

Membership operator

Identity operator

24. What is a global variable?

Global variables are the one that are defined and declared outside a function and we need to use them inside a
function.

Example:#This function uses global variable s

def f():

print(s)

# Global scope

s ="I love India"

f()

25. Define function.

A function in Python is defined by a def statement. The general syntax looks like this:

def function-name(Parameter list):

statements # the function body

The parameter list consists of zero or more parameters. Parameters are called arguments, if the function is
called. The function body consists of indented statements. The function body gets executed every time the
function is called. Parameter can be mandatory or optional. The optional parameters (zero or more) must follow
the mandatory parameters.

26. What is the purpose of using comment in python program?

Comments indicate information in a program that is meant for other programmers (or anyone reading thesource
code) and has no effect on the execution of the program. In Python, we use the hash (#) symbol to start writing a
comment.

Example: #This is a comment

27. State the reasons to divide programs into functions. (Jan-2019)

Creating a new function gives the opportunity to name a group of statements, which makes program easier to
read and debug. Functions can make a program smaller by eliminating repetitive code. Dividing a long program
into functions allows to debug the parts one at a time and then assemble them into a working whole. Well
designed functions are often useful for many programs.
28. Outline the logic to swap the content of two identifiers without using third variable. (May 2019)

a=10

b=20

a=a+b

b=a-b

a=a-b

print(“After Swapping a=”a,

” b=”

,b)

29. State about Logical operators available in python language with example. (May 2019)

Logical operators are the and, or, not operators.

Operator Meaning Example

And True if both the operands are true x and y

Or True if either of the operands is true x or y

Not True if operand is false (complements the operand) not x

30. Compare Interpreter and Compiler (Nov / Dec 2019)

Compiler : A Compiler is a program which translates the source code written in a high level language in to
object code which is in machine language program. Compiler reads the whole program written in high level
language and translates it to machine language. If any error is found, it display error message on the screen.

Interpreter : Interpreter translates the high level language program in line by line manner. The interpreter
translates a high level language statement in a source program to a machine code and executes it immediately
before translating the next statement. When an error is found the execution of the program is halted and error
message is displayed on the screen

31. Write a python program to circulate the values of n variables

no_of_terms = int(input("Enter number of values : "))

list1 = []

for val in range(0,no_of_terms,1):

ele = int(input("Enter integer : "))

[Link](ele)

print("Circulating the elements of list "

, list1)
for val in range(0,no_of_terms,1):

ele = [Link](0)

[Link](ele)

You might also like