Part 2 : Introduction to Python
Introduction
·
Python
was created by Guido van Rossum in 1991.
·
Widely
popular due to its simplicity, versatility, and ease of
learning
·
Python’s
syntax is simple and similar to everyday English,
making it easy for beginners.
·
Python
is named after Monty Python’s Flying Circus, a famous
BBC comedy show.
·
Web
development
·
Data
science & data analysis
·
Machine
learning & artificial intelligence
·
Automation
and scripting
·
Game
development
·
Web
scraping
·
Cyber
security
· Built-in Libraries & Tools
Features of
Python
a.
Easy to read and write
Python
uses simple and understandable syntax, making it easy for
programmers
to write and understand the code.
b.
Versatile
Python
can be used for a wide range of tasks like simple automating
systems
to complex web development, data analysis, and Artificial
Intelligence.
c.
Beginner-friendly
Python
uses simple syntax, making it a great choice for those who are new
to programming.
d.
Extensive standard library
Python
has an extensive standard library of pre-written code that offers
programmers
ready-made solutions, without requiring them to write
code
from ground level.
e.
Rich ecosystem
Python
has a vast collection of libraries and ready-made structures known
as
frameworks that provide ready-to-use tools for programmers.
The classification of data based on its nature is called data type.
Python also offers
various data type which are listed as:
1. Integer (int): It is a whole number ranging
from negative infinity to
positive infinity.
Examples: -,...,-3,-2,-1,0,1,2,3,....,
2. Float (float): It is numbers with decimals.
Examples: 3.14, -0.5, 1.567.
3. String (str): It consists of alphabets,
special characters,
alphanumeric values which are enclosed in double quotes.
Examples: “hello” , “Python@”,
“Mahendranagar1”,
“@#@#kathmandu”
4. Boolean (bool): It only provides True or
False values.
Example: is_student
= True, has_mobile=False
The print statement in Python is used to display information on
the screen. We use a ‘print’ statement to display the output of data that we
want to show.
Example: print(“Hello, Python!”)
The ‘input’ function allows us to provide input to the
program. We can give data to the program as we need.
Example: number
= input(“Enter number: ”)
String formatting in Python is like creating a special message
where you fill in the blanks with the information you want. It involves using a
message template, allowing you to insert different names or numbers at specific
spots to make your messages more interesting and customized.
1.
Old-style formatting
In earlier times, we
used ‘%’ to insert values into a string. This was the old way of formatting the
string values.
name= “Ram”
age = 25
print(“My name is %s and I am %d years old.” % (name, age))
Output: My name is Ram and I am 25 years old.
The new style string
format uses ‘{ }’ placeholders and the ‘format’ method. It is used in newer
generations of formatting.
name = “Sita”
age = 30
print(“My name is {} and I am {} years old.”.format(name, age))
Output: My name is Sita, and I am 30 years old
3.
Formatted string literals
This is the most
widely used method in string formatting in the current time. In formatted
string literal we use ‘f’ before the string and embedding expressions inside {
}. It is the most popular method of using string format.
#Formatted String Literals (f-strings)
name = “Shiva”
age = 35
print (f“My name is {name} and I am {age} years old.”)
Output: My name is Shiva, and I am 35 years old.
Operator
in Python:
1.
Arithmetic operator
Arithmetic operator
is used in Python to do mathematical operations. We use arithmetic operators as
special symbols to do basic math. It is like having a set of tools for simple
calculation.
Using
arithmetic operators in Python:
|
Operators
|
Description
|
Example
|
Output |
|
+ |
It
is used for addition. |
print(5
+ 3) |
8 |
|
- |
It
is used for subtraction. |
print(7
- 2) |
5 |
|
* |
It
is used for multiplication. |
print(4
* 6) |
24 |
|
/ |
It
is used for division. |
print(10
/ 2) |
5.0 |
|
% |
It
is used for modulus. |
print(5
% 3) |
2 |
|
**
|
It
is used for exponentials. |
print(3
** 2) |
9 |
Relational operator is used to check and compare values. These
operators check the relationship between two things and tell us if they are
equal, greater than or less than each other.
|
Name
|
Operator
|
Description
|
Example
|
|
Equal to |
==
|
Two
things are exactly the same |
(5
== 5) |
|
Not equal to |
!=
|
Two
things are not the same |
(3
!= 5) |
|
Greater than |
>
|
One
is greater than the other |
(7
> 5) |
|
Less than |
<
|
One
is less than the other |
(3
< 9) |
|
Greater than or equal to |
>=
|
Is
greater or equal to another |
(8
>= 8) |
|
Less than or equal to |
<=
|
Is
less or equal to another |
(4
<= 6) |
Logical operators in Python are used to combine conditions and
make decisions based on different situations. This operator is like a tool that
helps us make decisions based on different situations. There are 3 main logical
operators, ‘and’, ‘or’, and ‘not’.
AND:
Both the conditions must be true for the result to be true in the
“and” operator.
Example: x = (5<2) and (5>3)
Result: False
Truth table for ‘AND’ operators
|
A |
B |
A
and B |
|
0 |
0 |
0 |
|
0 |
1 |
0 |
|
1 |
0 |
0 |
|
1 |
1 |
1 |
Only one of the conditions needs to be true for the result to be
true in the “or” operator.
Example: (5<2) or (5>3)
Result: True
Truth table for ‘OR’ operators
|
A |
B |
A
and B |
|
0 |
0 |
0 |
|
0 |
1 |
1 |
|
1 |
0 |
1 |
|
1 |
1 |
1 |
NOT:
The logical operator “not” provides the opposite result of
a given condition.
Example: not(5<2)
Result:
True
Truth
table for ‘NOT’ operators
|
A |
Ā |
|
0 |
1 |
|
1 |
0 |
4. Assignment operator
Operator | Name | Example |
= | Assignment Operator | a = 7 |
+= | Addition Assignment | a += 1 # a = a + 1 |
-= | Subtraction Assignment | a -= 3 # a = a – 3 |
*= | Multiplication Assignment | a *= 4 # a = a * 4 |
/= | Division Assignment | a /= 3 # a = a / 3 |
%= | Remainder Assignment | a %= 10 # a = a % 10 |
**= | Exponent Assignment | a **= 10 # a = a ** 10 |
Book's Exercise Page number 254
Multiple Choice Questions (MCQs)
1. Python is developed by …………
i. Denish Ritchie
ii. James Ghosling
iii. Guido Van Rossum
iv. Charles Babbage
2. Python is based programming language
i. Assembler
ii. Interpreter
iii. Compiler
iv. Deassembler
3. Which of the following symbol is used for single line comment in Python?
i. &
ii. $
iii. ”
iv. #
4. Which of the following statement is used to display output in Python?
i. print
ii. printf
iii. console.log
iv. cout
5. Which of the following variable name is invalid in Python?
i. numl
ii. num_1
iii. Inum
iv.
num-1
6. Which of the following is not an arithmetic operator in Python?
i. +
ii. /
iii. ^
iv. #
7. Which of the following variable name is valid in Python?
i. $num
ii. num1
iii. num 1
iv. 1num
Technical Terms
1. The name given to memory location used to store value → Variable
2.
The set of rules that should be followed by programmers while writing programs
→ Syntax
3.
The words which have their own specific function in the program → Keyword
4.
The sign or symbols which performs specific operations on constants and
variables → Operator
5.
The combination of variables, constants, and operators → Expression
Short Answer Questions
a. Define Python programming language. Write any two characteristics.
Python is a high-level, interpreted
programming language developed by Guido Van Rossum.
Characteristics:
1. Easy to read and write (simple syntax)
2. Platform-independent
b.
What is an identifier? Write any two rules for naming identifier.
Identifier: A name given to a variable,
function, or object in Python.
Rules:
1. Must start with a letter or underscore (_)
2. Cannot contain spaces or special
characters
c.
What is a keyword? List any four keywords used in Python.
Keyword: Reserved words in Python that have a
specific meaning and cannot be used as identifiers.
Examples: `if`, `else`, `for`, `while`
d.
Write the function of input and print statements.
input(): Accepts input from the user
print(): Displays output on the screen
e.
What is type casting? List the different types of type casting.
Type casting: Converting one data type into
another
Types:
1. `int()` → converts to integer
2. `float()` → converts to float
3. `str()` → converts to string
f.
What is data type? List any two data types used in Python.
Data type: Classification of data which tells
the interpreter how the programmer intends to use the data.
Examples: `int`, `float`
g.
What is a variable?
A
variable is a named memory location used to store data that can be changed
during program execution.
Long Answer Questions
a.
What is an operator? Explain the different types of operators with examples.
Operator: A symbol that performs specific
operations on one or more operands.
Types
of Operators:
1.
Arithmetic Operators: `+`, `-`, ``, `/`, `%`, ``, `//`
Example: `5 + 3 = 8`
2.
Relational/Comparison Operators: `==`, `!=`, `>`, `<`, `>=`, `<=`
Example: `5 > 3 → True`
3.
Logical Operators: `and`, `or`, `not`
Example: `(5>3 and 3<2) → False`
4.
Assignment Operators: `=`, `+=`, `-=`, `=`, `/=`
Example: `x += 5` means `x = x + 5`
5.
Bitwise Operators: `&`, `|`, `^`, `~`, `<<`, `>>`
Example: `5 & 3 = 1`
6.
Membership Operators: `in`, `not in`
Example: `'a' in 'apple' → True`
7.
Identity Operators: `is`, `is not`
Example: `a is b`
b.
What is a conditional statement? Explain the different types of conditional
statements.
Conditional statement: A statement that
executes certain code based on a condition.
Types
of Conditional Statements in Python:
1.
if statement
if
x > 0:
print("Positive")
2.
if-else statement
if
x > 0:
print("Positive")
else:
print("Non-positive")
3.
if-elif-else statement
if
x > 0:
print("Positive")
elif
x == 0:
print("Zero")
else:
print("Negative")