Learn Python This Summer:Day 3: Data Types and Operators

Welcome back! Yesterday, we learned about Python’s basic syntax and variables. Today, we’ll explore data types and operators, which are essential for manipulating and working with data in your programs. By the end of this day, you’ll have a solid understanding of Python’s data types and how to use operators to perform various operations. Let’s get started!

Data Types
Python supports several built-in data types that are used to store and manipulate data. Here are the most common ones:

  1. Integers
    Integers are whole numbers, both positive and negative, without decimals.

python
Copy code
age = 14
score = -50

  1. Floats
    Floats are numbers that have a decimal point.

python
Copy code
height = 5.4
weight = 60.5

  1. Strings
    Strings are sequences of characters enclosed in quotes.

python
Copy code
name = "Andrew"
greeting = ‘Hello, World!’

  1. Booleans
    Booleans represent one of two values: True or False.

python
Copy code
is_student = True
is_tall = False

  1. Lists
    Lists are ordered collections of items, which can be of different data types.

python
Copy code
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]

  1. Tuples
    Tuples are similar to lists, but they are immutable, meaning they cannot be changed after creation.

python
Copy code
dimensions = (1920, 1080)
coordinates = (40.7128, -74.0060)

  1. Dictionaries
    Dictionaries are collections of key-value pairs.

python
Copy code
student = {"name": "Andrew", "age": 14, "grade": "9th"}
Operators
Operators are used to perform operations on variables and values. Here are some common types of operators in Python:

Arithmetic Operators

  • : Addition
  • : Subtraction
  • : Multiplication
    / : Division
    ** : Exponentiation
    % : Modulus
    Example:

python
Copy code
x = 10
y = 3

print(x + y) # 13
print(x – y) # 7
print(x * y) # 30
print(x / y) # 3.3333333333333335
print(x ** y) # 1000
print(x % y) # 1
Comparison Operators
== : Equal to
!= : Not equal to

: Greater than
< : Less than
= : Greater than or equal to
<= : Less than or equal to
Example:

python
Copy code
a = 5
b = 10

print(a == b) # False
print(a != b) # True
print(a > b) # False
print(a < b) # True
print(a >= b) # False
print(a <= b) # True
Logical Operators
and : Returns True if both statements are true
or : Returns True if one of the statements is true
not : Reverses the result, returns False if the result is true
Example:

python
Copy code
c = True
d = False

print(c and d) # False
print(c or d) # True
print(not c) # False
Assignment Operators
= : Assigns a value to a variable
+= : Adds and assigns a value
-= : Subtracts and assigns a value
*= : Multiplies and assigns a value
/= : Divides and assigns a value
Example:

python
Copy code
e = 10
e += 5 # e = e + 5
print(e) # 15

f = 10
f = 3 # f = f 3
print(f) # 30
Practice Time!
Let’s put what we’ve learned into practice. Try writing a Python program that declares variables of different types, performs some arithmetic and comparison operations, and prints the results.

python
Copy code

Declaring variables

num1 = 20
num2 = 4
name = "Andrew"
is_coding_fun = True

Arithmetic operations

sum_result = num1 + num2
diff_result = num1 – num2
product_result = num1 * num2
quotient_result = num1 / num2

Comparison operations

is_equal = num1 == num2
is_greater = num1 > num2

Logical operation

and_result = (num1 > 10) and (num2 < 10)

Printing results

print("Sum:", sum_result)
print("Difference:", diff_result)
print("Product:", product_result)
print("Quotient:", quotient_result)
print("Is equal:", is_equal)
print("Is greater:", is_greater)
print("Logical AND result:", and_result)
Conclusion
Awesome job today! You’ve learned about Python’s data types and operators, which are crucial for writing powerful programs. Tomorrow, we’ll dive into control structures like if statements and loops. Keep practicing and having fun coding!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *