0% found this document useful (0 votes)
4 views6 pages

2.1 - Python Basics

The document provides an overview of Python basics, covering variables, data types, and basic operations such as arithmetic, comparison, and logical operations. It also explains control structures including conditional statements and loops, along with loop control statements. Examples are provided for each concept to illustrate their usage in Python.

Uploaded by

faraisibandatt
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)
4 views6 pages

2.1 - Python Basics

The document provides an overview of Python basics, covering variables, data types, and basic operations such as arithmetic, comparison, and logical operations. It also explains control structures including conditional statements and loops, along with loop control statements. Examples are provided for each concept to illustrate their usage in Python.

Uploaded by

faraisibandatt
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

1: Python Basics

Variables and Data Types


Variables: Containers for storing data values. In Python, you don't need to
declare the variable type explicitly.

x = 5 # integer
y = "Hello" # string
z = 3.14 # float

Data Types:

Integers: Whole numbers, positive or negative, without decimals.

age = 25

Floats: Numbers, positive or negative, containing one or more decimals.

pi = 3.14159

Strings: Ordered sequences of characters, enclosed in single, double, or


triple quotes.

name = "Alice"

Booleans: Represents one of two values: True or False .

1: Python Basics 1
is_student = True

Lists: Ordered, mutable collections of items.

fruits = ["apple", "banana", "cherry"]

Tuples: Ordered, immutable collections of items.

coordinates = (10.0, 20.0)

Dictionaries: Unordered collections of key-value pairs.

person = {"name": "Alice", "age": 25}

Basic Operations
Arithmetic Operations:

Addition: +

Subtraction:

Multiplication:

Division: /

Modulus (remainder): %

Exponentiation: *

Floor Division: //

1: Python Basics 2
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333333333333335
print(a % b) # 1
print(a ** b) # 1000
print(a // b) # 3

Comparison Operations:

Equal: ==

Not Equal: !=

Greater Than: >

Less Than: <

Greater Than or Equal To: >=

Less Than or Equal To: <=

x = 5
y = 10
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True

Logical Operations:

1: Python Basics 3
And: and

Or: or

Not: not

a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False

Control Structures
Conditional Statements:

if , elif , else

x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")

Loops:

for Loop: Iterates over a sequence (like a list, tuple, dictionary, set, or
string).

fruits = ["apple", "banana", "cherry"]


for fruit in fruits:

1: Python Basics 4
print(fruit)

while Loop: Repeats as long as a condition is true.

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

Loop Control Statements:

break : Terminates the loop.

: Skips the rest of the code inside the loop for the current iteration
continue

and jumps to the next iteration.

pass : Does nothing; acts as a placeholder.

for i in range(10):
if i == 5:
break
print(i)
# 0 1 2 3 4

for i in range(10):
if i % 2 == 0:
continue
print(i)
# 1 3 5 7 9

for i in range(10):
if i % 2 == 0:
pass

1: Python Basics 5
print(i)
# 0 1 2 3 4 5 6 7 8 9

1: Python Basics 6

You might also like