0% found this document useful (0 votes)
3 views2 pages

Python Basics: Operators and Control Flow

The document provides an overview of Python basics, including types of operators, control flow statements, string immutability, leap year calculation, and function definition. It includes code examples demonstrating arithmetic, assignment, comparison, logical, bitwise, membership, and identity operators, as well as loops and functions. Each section illustrates key concepts with practical code snippets.
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)
3 views2 pages

Python Basics: Operators and Control Flow

The document provides an overview of Python basics, including types of operators, control flow statements, string immutability, leap year calculation, and function definition. It includes code examples demonstrating arithmetic, assignment, comparison, logical, bitwise, membership, and identity operators, as well as loops and functions. Each section illustrates key concepts with practical code snippets.
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

Python Basics - Examples with Code

1. Types of Operators in Python

# Arithmetic Operators
a = 10
b = 3
print("Addition:", a + b) # 13
print("Power:", a ** b) # 1000

# Assignment Operators
a += 5
print("After += :", a) # 15

# Comparison Operators
print("a > b:", a > b) # True

# Logical Operators
print("True and False:", True and False) # False

# Bitwise Operators
print("Bitwise AND:", 5 & 3) # 1

# Membership Operators
print("a in [5, 15]:", a in [5, 15]) # True

# Identity Operators
x = [1, 2]
y = x
print("x is y:", x is y) # True

2. Control Flow Statements

# if-elif-else and loops


num = 7
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")

# For loop
for i in range(3):
print("Loop:", i)

# While loop
count = 0
while count < 3:
print("While count:", count)
count += 1
Python Basics - Examples with Code

3. Strings in Python are Immutable

s = "python"
# s[0] = 'P' # This will cause an error

# Correct way:
s = "P" + s[1:]
print("Modified string:", s) # Python

4. Program to Find Leap Year or Not

year = int(input("Enter a year: "))


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")

# Example input/output:
# Enter a year: 2024
# 2024 is a leap year

5. Functions in Python

# Define a function
def add_numbers(a, b):
return a + b

# Call the function


result = add_numbers(5, 10)
print("Sum:", result) # 15

Common questions

Powered by AI

Arithmetic operators perform basic mathematical operations such as addition and exponentiation. An example is 'a + b', which would be 13 for a = 10 and b = 3. Assignment operators, on the other hand, modify the value of a variable using another value, such as 'a += 5' which updates 'a' to 15 .

A year is a leap year in Python if it is divisible by 4 but not by 100, unless it is divisible by 400. For instance, 2024 satisfies 'year % 4 == 0 and year % 100 != 0', making it a leap year .

The 'in' operator checks if a sequence contains an element. For instance, 'a in [5, 15]' evaluates to True if 'a' is either 5 or 15 .

Control flow statements like 'if-elif-else' guide the execution path based on conditions, such as turning a number check into printing 'Positive', 'Zero', or 'Negative'. Loops, like a 'for' loop with 'for i in range(3):', iterate over a sequence three times .

Logical operators in Python are used to evaluate expressions and return a Boolean result. The 'and' operator requires both conditions to be true, while the example 'True and False' evaluates to False as one condition is false .

Attempting 's[0] = 'P'' to change a string directly causes an error due to string immutability. To change a string, concatenate: 's = "P" + s[1:]', which correctly modifies 'python' to 'Python' .

A 'for' loop iterates over a fixed sequence, like 'for i in range(3):', which runs three times. Conversely, a 'while' loop continues until a condition changes, as 'while count < 3:' increments 'count' until it ceases to be less than 3 .

In Python, strings are immutable, meaning their characters cannot be changed in place. For example, trying 's[0] = 'P'' will cause an error. To modify a string, a new one must be created, like 's = "P" + s[1:]', which changes 'python' to 'Python' .

Identity operators like 'is' test if two references point to the same object. In the example, 'x is y' evaluates to True because 'x' and 'y' point to the same list object .

Python functions encapsulate code and handle specific tasks, defined using 'def'. Calling 'add_numbers(5, 10)' returns the sum of 5 and 10, which is 15, demonstrating encapsulation and functionality .

You might also like