[go: up one dir, main page]

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

Week 1

Uploaded by

parkerupsc
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)
8 views7 pages

Week 1

Uploaded by

parkerupsc
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/ 7

WEEK-1

Ques on 1
Convert the given integer x = 20 to a oat, append the oat to the pre exis ng list
values =[5, 10, 15], and solve the equa on .
Answer
x = 20
x_ oat = oat(x)
values = [5, 10, 15]
values.append(x_ oat)
y = 2 * x_ oat + 3

Ques on 2
Write a Python code snippet to declare a variable text of type str with the value
"Python",and another variable num of type int with the value 3. Concatenate text
and num such that the output is “Python3".
Answer:
text = "Python"
num = 3
result = text + str(num)
print(result)

Ques on 3
Explain the di erence between mutable and immutable data types in Python, with
examples.
Answer:
Mutable Data Types: These are data types whose values can be changed a er they
are created.Examples include lists, dic onaries, and sets.
Eg:
my_list = [1, 2, 3]
my_list[0] = 10
print(my_list)
Immutable Data Types: These are data types whose values cannot be changed once
they are created. Examples include strings, tuples, and integers.Eg:
my_tuple = (1, 2, 3)
fl
ti
ti
ti
fl
fl
ff
fl
fl
ti
ti
fl
ft
ti
Ques on 4:
Create a list of 10 natural numbers and make a new list have only even numbers
Answer:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = []
for num in numbers:
if num % 2 == 0:
even_numbers.append(num)
print(even_numbers)

Ques on 5:
In Python, both lists and dic onaries are mutable data structures. However, their
behavior and use cases di er signi cantly. Explain:
(a) How elements are accessed in lists versus dic onaries.
(b) Why dic onaries are more e cient for certain types of lookups.
Answer :
(a) Elements in a list are accessed using integer indices, star ng from 0, e.g.,
my_list[0] . In contrast, dic onaries use keys for access, which can be strings,
numbers, or other immutable types, e.g., my_dict['key'].
(b) Dic onaries use hash tables internally, enabling constant- me complexity (O(1))
for lookups, while lists require linear- me complexity (O(n)) since they search
sequen ally.

Ques on 6:
Consider the following Python code:
x = [1, 2, [3, 4]]
y = x.copy()
y[2][0] = 5
y.append(6)
print(x, y)
(a) Predict the output.
(b) Explain why the inner list in x was a ected but the outer list wasn’t.
Answer :
(a) Output:
[1, 2, [5, 4]] [1, 2, [5, 4], 6]
(b) The copy() method creates a shallow copy of x , so the outer list x remains
una ected by changes like appending 6 to y . However, the inner list [3, 4] is
shared between x and y , so modifying y[2][0] a ects x as well.
ff
ti
ti
ti
ti
ti
ti
ff
ti
ti
ffi
fi
ti
ff
ff
ti
ti
ti
Ques on 7
How is Python different from Java?
Answer :
Java is a sta cally typed programming language, meaning that the type
safety is checked during compila on (sta c compila on). Python is a dynamically
typed programming language, meaning that type safety is checked during run
me.
The sta c compila on of Java results in the code wri en in Java to be more
verbose and hence leads to longer me taken to develop code. Since Python is
dynamically typed the code tends to be much shorter and less verbose and hence
me taken to develop any code in Python is much less than that of Java.
Dynamically typed codes also tend to be much more readable due to them being
less verbose. Hence Python also is much more readable than Java.

Ques on 8
Write a Python code snippet to verify if a given variable is of type int. If the variable
is not of type int, coerce it into an integer and display its new value. Assume the
variable’s ini al value is a string representa on of a number.For example, if the
variable is "25", the output should be 25.
Answer:
var = "25"
if type(var) is not int:
var = int(var)
print(var)

Ques on 9
Write a Python code snippet to check if the number 7 exists in a list of integers. If it
exists,
print "7 is in the list", otherwise print "7 is not in the list".
Use the list: [1, 2, 3, 4, 5, 6, 7, 8, 9].
Answer:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
if 7 in numbers:
print("7 is in the list")
else:
print("7 is not in the list")
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
tt
Ques on 10
What is the hierarchy of arithme c operators in Python?
Answer:
The hierarchy of arithme c operators in Python, from highest to lowest
precedence, is:
Follows rule : PEMDAS
Parentheses ( ) - Opera ons within parentheses are evaluated rst.
Exponen a on ( ** ) - Next highest precedence for power calcula ons.
Mul plica on ( * ), Division ( / ), Remainder ( % ) - These opera ons are
evaluated a er exponen a on.
Addi on ( + ) and Subtrac on ( - ) - Lowest precedence among
arithme c operators.

Ques on 11
Brief note on variable?
Answer:
● An iden er containing
a known informa on,
● Informa on is referred to as
Value.
● Variable name points to
a memory address or a storage loca on and used
to reference the stored
Value.

Ques on 12
2)What is the output of the following code?
a = [1, 2, 3]
a.append(4)
print(a)
Answer:
Output: [1, 2, 3, 4]
The append() method adds the element 4 to the end of the
list.
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
ft
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
ti
Ques on 13
3) How do you nd the length of a string in Python?
Answer:
len():
Eg: len(“Data Science”) # Output: 12WEEK 1 FAQ

Ques on 14
Compare Python and Java in terms of typing and compila on.
Answer:
Python is dynamically typed, performing run me type safety checks, and has faster
code development due to less verbosity. Java is sta cally typed, with type safety
checks during compila on,leading to longer code development mes.

Ques on 15
Describe the hierarchy of arithme c operators in Python.
Answer:
In decreasing order of precedence—
Parentheses (), Exponen a on **
, Division /, Mul plica on *
, Addi on +, and Subtrac on -

Ques on 16
What are the primary programming paradigms supported by Python?
Answer:
Func onal, Structural, and Object-Oriented Programming (OOP)Team Members:

Ques on 17
What are Python's data types, and why are they important in programming?
Answer:
Python's data types de ne the type of value a variable can hold and determine the
opera ons that can be performed on it. Common data types include:
• int: Represents integers (e.g., 5, -10).
• oat: Represents decimal numbers (e.g., 3.14, -2.5).
fl
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
ti
ti
fi
ti
ti
ti
ti
ti
ti
ti
ti
• str: Represents text data (e.g., "Hello").
• bool: Represents logical values (True or False).
Importance:
> Data types ensure e cient memory use and prevent errors by restric ng
opera ons to valid
types.
> They help in selec ng appropriate methods and operators for computa on.

Ques on 18
Name all the four basic libraries in python used in data
science. (2M)
Answer:
▪ NumPy – Numerical Python
▪ Pandas – Dataframe Python
▪ Matplotlib -Visualiza on
▪ Sklearn – Machine Learning

Ques on 19
What are the naming conven ons for variables in Python, and why should one avoid
using one-character variable names?
Answer:
Common naming conven ons includes
1. camelCase
2. snake_case
3. PascalCase.
One-character variable names should be avoided as they are not descrip ve and are
typicallyreserved for looping constructs or func ons.

Ques on 20
Explain the concept of late binding in Python and how it a ects method lookups
during run me.
Answer:
Late binding in Python means that the method or a ribute names are resolved at
run me rather than at compile me. This allows Python to support dynamic typing
ti
ti
ti
ti
ti
ti
ti
ffi
ti
ti
ti
ti
ti
tt
ff
ti
ti
ti
and polymorphism, as methods are looked up by name during execu on, enabling
greater exibility in object behavior and allowing method overriding in inheritance

Ques on 21
Explain the signi cance of the astype() method in Pandas. How does it facilitate the
conversion ofdata types, and provide an example of conver ng a variable's data
type?
Answer:
The astype() method in Pandas is used to explicitly convert the data type of a column
or an en re DataFrame. This is especially useful when a column's data type is
misinterpreted during data import, such as a numeric column being read as an
object due to special characters or missing value

Ques on 22
What is Tuple? Men on its proper es with an example
Answer:
In Python, a tuple is an ordered, immutable sequence of elements. This means that
once a tuple is created, its contents cannot be changed. Tuples are de ned by
enclosing a comma-separated list of values within parentheses ().
Eg: a = (10, 20, 30)

Ques on 23
Men on any 2 di erences between lists and tuples
Answer:

LIST :
• Lists are mutable
• Slower than tuples
• The list is better for performing operations, such as insertion and deletion
• Lists consume more memory
• Lists have several built-in methods Unexpected changes and errors are
more likely to occur

TUPLE
• Tuples are immutable
• Tuples are faster because of read only nature
• A Tuple data type is appropriate for accessing the elements
• Tuple consumes less memory as compared to the list
• Tuple does not have many built-in methods
• Because tuples don’t change they are far less error-prone.
ti
ti
ti
ti
fl
ti
fi
ff
ti
ti
ti
ti
fi

You might also like