Unit 4 Notes
Unit 4 Notes
UNIT - 4
Introduction to Python Programming:
print("Hello, World!")
Syntax in Programming:
In programming, "syntax" refers to the set of rules that dictate how code should
be written to be considered valid and executable by a computer. Just like human
languages have grammar rules, programming languages have syntax rules. A
single mistake in the syntax can lead to errors and prevent the code from running
correctly.
Syntax of Python:
The syntax of Python refers to the specific rules and guidelines that must be
followed when writing Python code. Python's syntax is designed to be easy to
read and understand, which makes it popular among beginners and experienced
developers alike.
x = 10
y=5
sum = x + y
difference = x - y
product = x * y
quotient = x / y
print("Sum:", sum)
Ravikumar R SA Lab,Unit 4 Page |3
print("Difference:", difference)
print("Product:", product)
print("Quotient:", quotient)
age = 18
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
greet("Bob")
Comments in Python:
In Python, comments are non-executable lines of text that are used to add
explanations or notes within the code. Comments are intended for developers to
understand the code better and are ignored by the Python interpreter during
execution. They play a crucial role in making the code more readable and self-
explanatory.
'''
This is a multi-line comment.
It can span multiple lines.
'''
print("Hello, World!")
"""
This is another multi-line comment.
It can also span multiple lines.
"""
print("Hello, Python!")
Example Program with Comments:
# Input values
length = 5
width = 3
# Integer
x = 10
print(x, type(x)) # Output: 10 <class 'int'>
# Floating-point
y = 3.14
print(y, type(y)) # Output: 3.14 <class 'float'>
2. String:
str: Represents a sequence of characters enclosed within single (' ') or
double (" ") quotes. Example: name = "John"
name = "Alice"
print(name, type(name))
# Output: Alice <class 'str'>
3. Boolean:
bool: Represents the Boolean values True or False (used for logical
operations). Example: is_student = True
is_student = True
print(is_student, type(is_student))
# Output: True <class 'bool'>
4. Sequence Types:
1. list: Represents an ordered collection of elements, enclosed within square
brackets [ ]. Example: my_list = [1, 2, 3, 4, 5]
# Tuple
coordinates = (10, 20)
print(coordinates, type(coordinates))
# Output: (10, 20) <class 'tuple'>
5. Mapping Type: dict: Represents a collection of key-value pairs, enclosed
within curly braces { }. Example: my_dict = {'name': 'John', 'age': 25}
# Output: {'name': 'John', 'age': 30, 'city': 'New York'} <class 'dict'>
6. Set Types:
1. set: Represents an unordered collection of unique elements, enclosed
within
curly braces { }. Example: my_set = {1, 2, 3, 4, 5}
2. frozenset: Represents an immutable version of a set. Example:
my_frozenset = frozenset({1, 2, 3})
# Set
unique_numbers = {1, 2, 3, 4, 5}
print(unique_numbers, type(unique_numbers))
# Frozenset
immutable_numbers = frozenset({1, 2, 3})
print(immutable_numbers, type(immutable_numbers))
result = None
print(result, type(result)) # Output: None <class 'NoneType'>
Ravikumar R SA Lab,Unit 4 P a g e | 11
Introduction to If-Else Statements in Python
1. Introduction to Conditional Statements: Conditional statements in
programming allow us to make decisions based on certain conditions. One of
the most fundamental forms of a conditional statement is the if-else statement.
In Python, the if-else statement is used to execute different blocks of code
based on whether a given condition is True or False.
2. Syntax of the If-Else Statement:
The general syntax of the if-else statement in Python is as follows:
if condition:
# Code to be executed if the condition is True
else:
# Code to be executed if the condition is False
# Input
num = int(input("Enter a number: "))
# Condition check using if-else
if num % 2 == 0:
print(num, "is even.")
else:
print(num, "is odd.")
# Input
Ravikumar R SA Lab,Unit 4 P a g e | 13
num = int(input("Enter a number: "))
# Input
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
# Input
age = int(input("Enter your age: "))
Ravikumar R SA Lab,Unit 4 P a g e | 15
# Input
year = int(input("Enter a year: "))
3. Syntax of for Loop: The general syntax of the for loop in Python is as follows:
print("Sum:", sum)
6. while Loop: The while loop is used to repeatedly execute a block of code as
long as a given condition is True.
7. Syntax of while Loop: The general syntax of the while loop in Python is as
follows:
while condition:
# Code to be executed while the condition is True
# Countdown timer
count = 5
while count > 0:
print(count)
count -= 1
print("Blast off!")
while True:
user_input = input("Enter the password: ")
if user_input == password:
print("Access granted!")
break # Exit the loop
else:
print("Access denied. Try again.")
10. Infinite Loops and Loop Control: Be cautious with while loops, as they can
potentially result in infinite loops if not properly controlled. You can use the
break statement to exit a loop prematurely, and the continue statement to skip
the current iteration and proceed to the next.
# Creating a list
fruits = ["apple", "banana", "orange", "grape"]
# Accessing elements
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: orange
def function_name(parameters):
# Function code
return result