Python Chapter 1
Python Chapter 1
Introduction to Python:
Python is a high-level, interpreted programming language known for its readability and
simplicity. Guido van Rossum created Python, and it was first released in 1991. Python's
design philosophy emphasizes code readability, and its syntax allows programmers to
express concepts in fewer lines of code than languages like C++ or Java.
Python Features:
2. **Versatile:**
Python is a general-purpose language used in various domains such as web
development, data science, machine learning, artificial intelligence, automation, and
more.
3. **Interpreted:**
Python is an interpreted language, which means that code can be executed line by
line, making it easy for debugging and testing.
4. **Dynamically Typed:**
Python is dynamically typed, meaning you don't need to specify the data type of a
variable explicitly. This makes development faster and code more flexible.
5. **Extensive Standard Library:**
Python comes with a vast collection of libraries and modules, providing pre-built
functions for a wide range of tasks.
6. **Community Support:**
Python has a large and active community of developers. This community support
includes forums, documentation, and numerous third-party libraries.
6. **Scientific Computing:**
Python is utilized in scientific computing applications due to its extensive libraries and
ease of integration with other languages.
7. **Desktop GUI Applications:**
Tools like Tkinter allow developers to create graphical user interfaces for desktop
applications.
Keywords:
Keywords are reserved words in Python and cannot be used as identifiers (variable
names, function names, etc.). Examples include if, else, for, while, True, False, None,
etc.
if True:
print("This is an example of a keyword.")
Identifiers:
Identifiers are names given to entities like variables, functions, etc. They should follow
certain rules, such as starting with a letter or underscore, and can include letters, digits,
and underscores.
variable_name = 42
Data Types:
Python has several built-in data types, including int, float, str (string), bool (boolean),
list, tuple, dict (dictionary), and more.
integer_variable = 42
float_variable = 3.14
string_variable = "Hello, Python!"
Boolean variable = True
Variables:
Variables are used to store values. They are created by assigning a value to an identifier.
x = 10
y=5
sum_result = x + y
print("Sum:", sum_result)
Operators:
a = 10
b = 20
# Arithmetic operators
sum_result = a + b
difference = a - b
product = a * b
quotient = a / b
remainder = a % b
# Comparison operators
is_equal = (a == b)
is_not_equal = (a != b)
is_greater = (a > b)
# Logical operators
logical_and = (a > 0) and (b > 0)
logical_or = (a > 0) or (b > 0)
logical_not = not (a > 0)
# Print results
print("Sum:", sum_result)
print("Is equal?", is_equal)
print("Logical AND:", logical_and)
Type Casting:
Type casting involves converting one data type to another. Python supports functions
like int(), float(), str(), etc., for type casting.
The print() function is used to display output in Python. It can print strings, variables,
and expressions.
You can combine input and output functions to create interactive programs.
1.6 Decision Making Structures: if, if-else statements, Nested if-else and if-elif-
else statements
. if Statement:
• The if statement is used for decision-making in Python. It executes a block of
code only if a specified condition is true.
x = 10
if x > 0:
print("Positive number")
2. if-else Statement:
• The if-else statement allows you to execute one block of code if the condition is true
and another if it's false.
y = -5
if y > 0:
print("Positive number")
else:
print("Non-positive number")
3. Nested if-else Statement:
• You can nest if-else statements inside each other for more complex decision-
making.
x = 10
if x > 0:
if x % 2 == 0:
print("Positive even number")
else:
print("Positive odd number")
else:
print("Non-positive number")
4. if-elif-else Statement:
• The if-elif-else statement allows you to check multiple conditions in sequence.
grade = 85
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
else:
print("C")
3. Nested Loops:
• You can nest loops inside each other to create more complex patterns or iterate
through multi-dimensional data structures.
# Example of nested loops
for i in range(3):
for j in range(2):
print(i, j)
3. pass Statement:
• The pass statement is a null operation, and it is used when a statement is
syntactically required but you do not want to perform any action.
# Example of pass statement
for i in range(3):
pass # Placeholder statement, no action is taken