4. Introduction to Python Programming23
4. Introduction to Python Programming23
Ramanagara
Diploma CET[Statistics and Analytics] Date : 22/07/2023
IV. Introduction to Python Programming[6Marks]
Python is a popular programming language. It other programming languages.
was created by Guido vanRossum, and released Python runs on an interpreter system,
in 1991. meaning that code can be executed as soon
It is used for: as it is written. This means that prototyping
web development (server-side) can be very quick.
software development Python can be treated in a procedural way,
mathematics an object-oriented way or afunctional way.
system scripting. Python Syntax compared to other
What can Python do? programming languages
Python was designed for readability, and has
Python can be used on a server to create web
some similarities to the English language
applications.
with influence from mathematics.
Python can be used alongside software to
Python uses new lines to complete a
create workflows.
command, as opposed to other programming
Python can connect to database systems. It
languages which often use semicolons or
can also read and modify files.
parentheses.
Python can be used to handle big data and
Python relies on indentation, using
perform complex mathematics.
whitespace, to define scope; such as the
Python can be used for rapid
scope of loops, functions and classes. Other
prototyping, or for
programming languages often use curly-
production-ready software
brackets for this purpose.
development.
Python Syntax
Why Python?
Python syntax can be executed by writing
Python works on different
directly in the Command Line:
platforms (Windows, Mac,
>>> print("Hello, World!")
Linux, RaspberryPi, etc).
Or by creating a python file on the server,
Python has a simple syntax similar to the
using the .py file extension, and running it in
English language.
the Command Line:
Python has syntax that allows developers to
C:\Users\Your Name>python myfile.py
write programs with fewer lines than some
print("Hello, print(
World!") x)
print(
Comments can be placed at the end of a line,
and Python will ignore the rest ofthe line: y)
print at,
co
(y)
mp
print
lex
(z)
Sequence list
And you can assign the same value to multiple
Types: ,
variables in one linex = y
tup
= z = "Orange"
le,
print(x) ran
print ge
(y)
x = memoryview
memoryview(byte
Example Data Type
s(5))
x = "Hello World" str
x = None NoneType
x = 20 int
The if statement
x = 20.5 float The if statement is used to test a particular
condition and if the condition is true, it executes
x = 1j complex
a block of code known as if-block. The
condition of if statement can be any valid
x = ["apple", "banana", list logical expression which can be either evaluated
"cherry"] to true or false.
x = range(6) range
x = frozenset({"apple", frozenset
"banana", "cherry"}) The syntax of the if-statement is given below.
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
if condition:
#block of statements
else:
#another block of statements (else-block)
Example 1 : Program to check whether a person
is eligible to vote or not.
3) 3.0 1) int
4) 3.333 2) float
8. What is the data type of the result of the 3) tuple
expression: 10 / 3? 4) dictionary
1) int
13. What is the output of the following code
2) float
snippet?
3) str
``` python
4) bool
my_tuple = (1, 2, 3, 4)
9. Which of the following data types is mutable
print(my_tuple[2])
in Python?
1) int ```
2) float 1) 1
3) tuple 2) 2
4) list 3) 3
10. What is the purpose of the None data type 4) 4
in Python? 14. Which data type is used to store an
1) It represents an empty value or no value at unordered collection of unique elements in
Python?
all
1) int
2) It represents a Boolean value
2) float
3) It represents a numeric value
3) str
4) It represents a string value
4) set
y = 10 2) float
3) str
x = y print(x)
4) bool
``` 9. What is the result of the following
1) 5 expression?
2) 10
``` python x = 5,
3) Error: invalid syntax
Y=2
4) Error: variable names cannot be reassigned
result= x % y print(result)
5. What is the purpose of variable naming
conventions in Python? ```
1) To confuse other developers 1) 2.5
2) To indicate the data type of the variable 2) 0.5
3) To make the code more readable and 3) 0
understandable 4) 1
4) To prevent the variable from being modified 10. Which of the following is a reserved
6. What is the output of the following code keyword in Python and cannot be used as a
snippet? variable name?
``` python name = "John"
1) var
print("Hello, " + name) 2) if
``` 3) def
1) Hello, 4) try
2) Hello, John 12. What is the output of the following code
3) name snippet?
4) Error: invalid syntax
snippet? ```
1) 7
``` python x= 5
2) 52
y=2
3) Error: unsupported operand type(s) for +:
result = x// y print(result)
'str' and 'int'
```
4) Error: invalid syntax
``` ```
1) [1, 2, 3, 4]
1) [1, 2, 3, 4]
2) [1, 2, 4]
2) [1, 2, 5, 4]
3) [3, 2, 1]
3) [1, 2, 5]
4) [1, 2, 3]
4) [1, 5, 3, 4]
14. What is the purpose of the sort() method for
10. Which of the following is a method used to
arrays in Python?
add an element to an array in Python?
1) To remove duplicate elements from the
1) append()
array
2) remove()
2) To reverse the order of elements in the
3) pop()
array
4) sort()
3) To sort the elements in ascending or
11. What is the output of the following code
descending order
snippet?
4) To find the index of a specific element in
```python
the array
``` print(element)
1) [3, 2, 1, 4]
```
2) [4, 3, 2, 1]
3) [1, 2, 3, 4] 1) 1
4) [1, 4, 2, 3] 2) 2
1) append() Python?
1) To return the number of elements in an
2) remove()
3) pop() array
4) 4 4) array
18. What is the purpose of using loops with 2. What is the data type of the variable "x" in the
```
2) False 1) if statement
1. Which control statement is used to make 5. What is the purpose of the for loop in
2. What is the purpose of the if-else statement in 6. What is the output of the following code
Python? snippet?