0% found this document useful (0 votes)
6 views

What is Slicing in Python

The document provides an overview of various Python concepts including slicing, modules, packages, built-in data types, and scope. It explains the syntax for slicing, the advantages of modular programming, and categorizes data types such as NoneType, numeric types, and sequence types. Additionally, it covers the characteristics of interpreted languages and includes multiple-choice questions related to Python operators and data types.

Uploaded by

yashrj90062
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)
6 views

What is Slicing in Python

The document provides an overview of various Python concepts including slicing, modules, packages, built-in data types, and scope. It explains the syntax for slicing, the advantages of modular programming, and categorizes data types such as NoneType, numeric types, and sequence types. Additionally, it covers the characteristics of interpreted languages and includes multiple-choice questions related to Python operators and data types.

Uploaded by

yashrj90062
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
You are on page 1/ 8

1. What is slicing in Python?

 As the name suggests, ‘slicing’ is taking parts of.


 Syntax for slicing is [start : stop : step]
 start is the starting index from where to slice a list or tuple
 stop is the ending index or where to sop.
 step is the number of steps to jump.
 Default value for start is 0, stop is number of items, step is 1.
 Slicing can be done on strings, arrays, lists, and tuples.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(numbers[1 : : 2]) #output : [2, 4, 6, 8, 10]
2. What are modules and packages in Python?
Python packages and Python modules are two mechanisms that allow for modular
programming in Python. Modularizing has several advantages -
 Simplicity: Working on a single module helps you focus on a relatively small portion of
the problem at hand. This makes development easier and less error-prone.
 Maintainability: Modules are designed to enforce logical boundaries between different
problem domains. If they are written in a manner that reduces interdependency, it is
less likely that modifications in a module might impact other parts of the program.
 Reusability: Functions defined in a module can be easily reused by other parts of the
application.
 Scoping: Modules typically define a separate namespace, which helps avoid confusion
between identifiers from other parts of the program.
Modules, in general, are simply Python files with a .py extension and can have a set of
functions, classes, or variables defined and implemented. They can be imported and initialized
once using the import statement. If partial functionality is needed, import the requisite classes
or functions using from foo import bar.
Packages allow for hierarchial structuring of the module namespace using dot notation.
As, modules help avoid clashes between global variable names, in a similar
manner, packages help avoid clashes between module names.
Creating a package is easy since it makes use of the system's inherent file structure. So just
stuff the modules into a folder and there you have it, the folder name as the package name.
Importing a module or its contents from this package requires the package name as prefix to
the module name joined by a dot.
3. What are the common built-in data types in Python?
There are several built-in data types in Python. Although, Python doesn't require data types to
be defined explicitly during variable declarations type errors are likely to occur if the
knowledge of data types and their compatibility with each other are neglected. Python
provides type() and isinstance() functions to check the type of these variables. These data
types can be grouped into the following categories-
 None Type:
None keyword represents the null values in Python. Boolean equality operation can be
performed using these NoneType objects.

Class Name Description

NoneType Represents the NULL values in Python.

 Numeric Types:
There are three distinct numeric types - integers, floating-point numbers, and complex
numbers. Additionally, booleans are a sub-type of integers.

Class Name Description

int Stores integer literals including hex, octal and binary numbers as integers

Stores literals containing decimal values and/or exponent signs as floating-point


float
numbers

complex Stores complex numbers in the form (A + Bj) and has attributes: real and imag

bool Stores boolean value (True or False).

Note: The standard library also includes fractions to store rational numbers and decimal to
store floating-point numbers with user-defined precision.
 Sequence Types:
According to Python Docs, there are three basic Sequence Types - lists,
tuples, and range objects. Sequence types have the in and not in operators defined for
their traversing their elements. These operators share the same priority as the
comparison operations.

Class Name Description

list Mutable sequence used to store collection of items.

tuple Immutable sequence used to store collection of items.

range Represents an immutable sequence of numbers generated during execution.

str Immutable sequence of Unicode code points to store textual data.

What is Scope in Python?


Every object in Python functions within a scope. A scope is a block of code where an object in
Python remains relevant. Namespaces uniquely identify all the objects inside a program.
However, these namespaces also have a scope defined for them where you could use their
objects without any prefix. A few examples of scope created during code execution in Python
are as follows:
 A local scope refers to the local objects available in the current function.
 A global scope refers to the objects available throughout the code execution since
their inception.
 A module-level scope refers to the global objects of the current module accessible in
the program.
 An outermost scope refers to all the built-in names callable in the program. The
objects in this scope are searched last to find the name referenced.
Note: Local scope objects can be synced with global scope objects using keywords such
as global.
What is an Interpreted language?
An Interpreted language executes its statements line by line. Languages such as Python,
Javascript, R, PHP, and Ruby are prime examples of Interpreted languages. Programs written
in an interpreted language runs directly from the source code, with no intermediary
compilation step.

1. What is the assignment operator in Python?


A.==
B.=
C.:=
D.><
Answer: Option B
2. Which of the following is not a keyword?
A.eval
B.assert
C.nonlocal
D.pass
Answer: Option A
3. Which of the following is a valid variable name in Python?
A.my-variable
B.123variable
C._my_variable
D.$variable
4. What is the maximum possible length of an identifier?
A.31 characters
B.63 characters
C.79 characters
D.none of the mentioned
1. Which operator is used for exponentiation in Python?
A.^
B.**
C.^^
D.//
Answer: Option B
2. What is the result of 'True and False' ?
A.True
B.False
C.None
D.Error
Answer: Option B
3. What is the result of 'not True' ?
A.True
B.False
C.None
D.Error
Answer: Option B
4. What is the result of 'True or False' ?
A.True
B.False
C.None
D.Error
5. What does the 'in' operator do in Python?
A.Checks if two values are equal
B.Checks if a value is greater than another
C.Checks if a value is in a sequence
D.Checks if a value is not in a sequence
Answer: Option C
6. What does the 'not in' operator do in Python?
A.Checks if two values are equal
B.Checks if a value is greater than another
C.Checks if a value is not in a sequence
D.Checks if a value is in a sequence
Answer: Option C

13.
Which is the correct operator for power(xy)?
A.X^y

B.X**y

C.X^^y

D.None of the mentioned

Answer: Option B

What is the answer to this expression, 22 % 3 is?


A.7
B.1
C.0
D.5
Answer: Option B
17.
What is the result of the expression 7 % 3 ?
A.2.33
B.2
C.0.333
D.1
Answer: Option D
18.
What is the order of precedence for the arithmetic operators in Python?
A.+, *, /, -
B./, *, +, -
C.*, /, +, -
D.+, -, *, /
Answer: Option C
1.
Which of the following is a valid data type in Python?
A.int
B.num
C.digit
D.number
Answer: Option A
Solution:
The data type for integers in Python is int.

2.
What is the result of type(3.14) ?
A.float
B.int
C.double
D.numeric
Answer: Option A
Solution:
The type() function returns the data type of the argument.
3.
What data type is used to represent a sequence of characters in Python?
A.string
B.char
C.text
D.str
What is the purpose of the str() function in Python?
A.To convert a string to a number
B.To format strings
C.To concatenate strings
D.To convert a number to a string
Answer: Option D
The str() function converts a number or other data type to a string.
5.
Which of these in not a core data type?
A.Lists
B.Dictionary
C.Tuples
D.Class
Answer: Option D
6.
Given a function that does not return any value, What value is thrown by default when
executed in shell.
A.int
B.bool
C.void
D.None
Answer & Solution
Answer: Option D
What is the result of type(True) ?
A.true
B.bool
C.boolean
D.Boolean
Answer: Option B
Solution:
The data type for boolean values in Python is bool.
8.
In order to store values in terms of key and value we use what core data type.
A.list
B.tuple
C.class
D.dictionary
Answer: Option D
How do you create a floating-point number in Python?
A.5.0
B.5
C."5.0"
D.float(5)

What is the result of int(3.9) ?


A.3
B.3.9
C.4
D.4.0
Answer: Option A

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy