0% found this document useful (0 votes)
5 views

Python Basics Simplified (1)

The document provides a comprehensive introduction to Python programming, covering basic concepts such as writing simple programs, using comments, and understanding literals. It also explains various operators, control flow statements like if-else and loops, and includes exercises for practice. Overall, it serves as a beginner-friendly guide to essential Python programming skills.

Uploaded by

since2024gaming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Basics Simplified (1)

The document provides a comprehensive introduction to Python programming, covering basic concepts such as writing simple programs, using comments, and understanding literals. It also explains various operators, control flow statements like if-else and loops, and includes exercises for practice. Overall, it serves as a beginner-friendly guide to essential Python programming skills.

Uploaded by

since2024gaming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Python Basics Simplified

1. Writing Your First Python Program

Explanation

●​ Python is a beginner-friendly programming language.


●​ It is easy to read and write.
●​ A simple program prints text to the screen.

Syntax
print("Hello, World!")

Example
print("Welcome to Python Basics!")

5 Easy Exercises

1.​ Print your name using Python.


2.​ Print "Python is fun!".
3.​ Print the sum of 5 and 10.
4.​ Print the result of 20 minus 8.
5.​ Print "I am learning Python!" three times.
2. Comments in Python
Definition

Comments are lines in the code that are ignored by Python. They help explain the code.

Explanation

●​ Used to add notes for better understanding.


●​ Python ignores comments during execution.
●​ There are two types of comments:
○​ Single-line comment using #
○​ Multi-line comment using ''' or """

Syntax
# This is a single-line comment
''' This is a
multi-line comment '''

Example
# Printing a message
print("Hello, World!") # This prints Hello, World!
5 Easy Exercises

1.​ Add a comment before printing your name.


2.​ Write a multi-line comment about Python.
3.​ Add a comment explaining a sum operation.
4.​ Add a comment before a print statement.
5.​ Explain your Python program using comments.

3. Literals in Python
Definition

Literals are constant values assigned to variables in Python.

Explanation

●​ String Literals: Sequence of characters enclosed in quotes.


●​ Numeric Literals: Integer, float, and complex numbers.
●​ Boolean Literals: True or False values.
●​ Special Literal: None represents an empty value.

Example
name = "Alice" # String literal
age = 25 # Integer literal
pi = 3.14 # Float literal
complex_num = 2 + 3j # Complex number literal
is_student = True # Boolean literal
empty_value = None # Special literal

5 Easy Exercises

1.​ Assign a string literal to a variable and print it.


2.​ Store an integer and a float and print them.
3.​ Use a boolean literal in a condition.
4.​ Assign None to a variable and check its type.
5.​ Create a variable with a complex number.
All Operators in Python
Definition

Operators in Python are special symbols that perform operations on variables and values. They
are used for arithmetic calculations, comparisons, logical operations, and more.

Types of Operators in Python

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations like addition,


subtraction, multiplication, and division.

●​ + (Addition): Adds two values.


●​ - (Subtraction): Subtracts the second value from the first.
●​ * (Multiplication): Multiplies two values.
●​ / (Division): Divides the first value by the second.
●​ % (Modulus): Returns the remainder of division.
●​ ** (Exponentiation): Raises a value to the power of another.
●​ // (Floor Division): Divides and rounds down to the nearest integer.

Example

x = 10

y=5

print(x + y) # Addition: 15

print(x - y) # Subtraction: 5

print(x * y) # Multiplication: 50

print(x / y) # Division: 2.0

print(x % y) # Modulus: 0

print(x ** y) # Exponentiation: 100000

print(x // y) # Floor Division: 2


2. Comparison Operators

Comparison operators compare two values and return True or False.

●​ == (Equal to): Checks if two values are equal.


●​ != (Not equal to): Checks if values are different.
●​ > (Greater than): Checks if the left value is greater.
●​ < (Less than): Checks if the right value is greater.
●​ >= (Greater than or equal to): Checks if the left value is greater or equal.
●​ <= (Less than or equal to): Checks if the right value is greater or equal.

Example:

a = 10

b = 20

print(a == b) # False

print(a != b) # True

print(a > b) # False

print(a < b) # True

print(a >= b) # False

print(a <= b) # True

3. Logical Operators

Logical operators are used to combine multiple conditions.

●​ and: Returns True if both conditions are True.


●​ or: Returns True if at least one condition is True.
●​ not: Reverses the logical value.
Example:

x = True

y = False

print(x and y) # False (Both must be True)

print(x or y) # True (At least one is True)

print(not x) # False (Reverses True to False)

4. Bitwise Operators

Bitwise operators perform operations on binary numbers.

●​ & (AND): Sets each bit to 1 if both bits are 1.


●​ | (OR): Sets each bit to 1 if at least one bit is 1.
●​ ^ (XOR): Sets each bit to 1 if only one bit is 1.
●​ ~ (NOT): Inverts all bits.
●​ << (Left Shift): Shifts bits left, multiplying by 2.
●​ >> (Right Shift): Shifts bits right, dividing by 2.

Example:

x = 5 # 0101 in binary

y = 3 # 0011 in binary

print(x & y) # Bitwise AND: 1 (0001)

print(x | y) # Bitwise OR: 7 (0111)

print(x ^ y) # Bitwise XOR: 6 (0110)

print(~x) # Bitwise NOT: -6

print(x << 1) # Left shift: 10 (1010)

print(x >> 1) # Right shift: 2 (0010)


5. Assignment Operators

Assignment operators assign values to variables and modify them.

●​ = (Assigns value to variable).


●​ += (Adds value and assigns result).
●​ -= (Subtracts value and assigns result).
●​ *= (Multiplies and assigns result).
●​ /= (Divides and assigns result).
●​ %= (Finds remainder and assigns result).

Example:

x = 10

x += 5 # x = x + 5 -> 15

x -= 3 # x = x - 3 -> 12

x *= 2 # x = x * 2 -> 24

x /= 4 # x = x / 4 -> 6.0

x %= 2 # x = x % 2 -> 0.0

6. Identity Operators

Identity operators check whether two variables refer to the same object in memory.

●​ is: Returns True if objects are the same.


●​ is not: Returns True if objects are different.

Example:

x = [1, 2, 3]

y=x

z = [1, 2, 3]

print(x is y) # True (Same memory location)

print(x is z) # False (Different memory locations)

print(x is not z) # True


7. Membership Operators

Membership operators check whether a value exists within a sequence.

●​ in: Returns True if the value exists.


●​ not in: Returns True if the value does not exist.

Example

word = "Python"

print("P" in word) # True (P is in Python)

print("x" not in word) # True (x is not in Python)

5 Exercises

1.​ Perform addition, subtraction, and multiplication on two numbers.


2.​ Compare two numbers using >= and !=.
3.​ Use logical operators to check conditions.
4.​ Perform bitwise AND and OR operations.
5.​ Check if an element exists in a list using in.
4. If-Else Statements
Definition

If-Else statements are used to execute different blocks of code based on conditions.

Explanation

●​ if executes when the condition is True.


●​ else executes when the condition is False.

Syntax
if condition:
statement
else:
statement

Example
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")

5 Easy Exercises

1.​ Check if a number is positive or negative.


2.​ Print if a number is even or odd.
3.​ Check if a person is eligible to vote.
4.​ Compare two numbers and print the larger one.
5.​ Check if a word is "Python" and print a message.
5. If-Elif-Else Ladder
Definition

The if-elif-else ladder is used to check multiple conditions.

Explanation

●​ if checks the first condition.


●​ elif checks additional conditions.
●​ else executes when no conditions are met.

Syntax
if condition1:
statement1
elif condition2:
statement2
else:
statement3

Example
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
else:
print("Grade: F")

5 Easy Exercises

1.​ Assign a grade based on marks.


2.​ Categorize age groups (Child, Teen, Adult, Senior).
3.​ Check temperature levels and print messages accordingly.
4.​ Print different greetings based on the time of the day.
5.​ Create a basic chatbot with multiple responses.
6. Loops in Python
Definition

Loops are used to execute a block of code multiple times.

Types of Loops

●​ For Loop: Iterates over a sequence.


●​ While Loop: Runs while a condition is True.

Syntax
For loop:
for variable in sequence:
statement
While loop:​
​ ​ while condition:
statement

Examples
# For loop example
for i in range(5):
print(i)

# While loop example


count = 0
while count < 5:
print(count)
count += 1

5 Easy Exercises

1.​ Print numbers from 1 to 10 using a for loop.


2.​ Print even numbers from 2 to 20 using a while loop.
3.​ Create a loop to print the square of numbers from 1 to 5.
4.​ Use a loop to print each letter of a word.
5.​ Implement a countdown timer using a while loop.
6.​
7. Loop Manipulation Statements
Definition

Loop manipulation statements control the execution of loops.

Types

●​ break: Exits the loop prematurely.


●​ continue: Skips the current iteration.
●​ pass: Acts as a placeholder.
●​ Else: Execute at end of loop

Syntax
for i in range(5):
if i == 3:
break # Exits the loop at i = 3
print(i)

Examples
# Break Example
for i in range(5):
if i == 3:
break
print(i)

# Continue Example
for i in range(5):
if i == 2:
continue
print(i)

# Pass Example
for i in range(5):
if i == 2:
pass # Placeholder
print(i)

5 Easy Exercises
1.​ Use a break statement to exit a loop at a specific value.
2.​ Use continue to skip printing a specific number.
3.​ Implement a pass statement in a loop.
4.​ Create a loop with break and continue conditions.
5.​ Build a loop with a condition that stops at user input.

You might also like