Hello and welcome back to Day 3 of our Python learning journey! Today, we're rolling up our sleeves and getting our hands dirty with the fundamental building blocks of Python programming: Variables, Data Types, and Basic Operations.
Getting Python on Board
Before we dive into the exciting world of Python syntax, let's ensure our ship is sailing smoothly. If you haven't installed Python on your Ubuntu machine, worry not! Open up your terminal and type the following commands:
sudo apt update
sudo apt install python3
Congratulations! Python is now installed on your system.
Variables: The Storage Units of Python
Think of variables as storage units in the world of Python. They store information that your program can later use. Creating a variable in Python is as simple as giving it a name and assigning a value.
# Let's create a variable named 'greeting' and assign it the value 'Hello, Python!'
greeting = "Hello, Python!"
print(greeting)
Output:
Hello, Python!
Here, greeting is our variable, and its value is the friendly phrase "Hello, Python!".
Data Types: Python's Building Blocks
Python is smart; it understands different types of data. Let's explore some common data types:
1. Integers (int): Whole numbers without decimal points.
age = 25
print(age)
Output:
25
2. Floats (float): Numbers with decimal points.
height = 5.8
print(height)
Output:
5.8
3. Strings (str): Textual data enclosed in single or double quotes.
name = "Pythonista"
print(name)
Output:
Pythonista
Basic Operations: Crunching the Numbers
Python allows us to perform basic operations on our variables. Let's do some number crunching:
# Adding two numbers
num1 = 10
num2 = 5
sum_result = num1 + num2
print("Sum:", sum_result)
# Multiplying numbers
product_result = num1 * num2
print("Product:", product_result)
Output:
Sum: 15
Product: 50
Here, we've added two numbers and multiplied them. Python makes it effortless!
In just a short span, we've navigated through the basics of Python syntax. We created variables, explored different data types, and performed basic operations. Exciting, isn't it?
Remember, practice is key! Play around with the examples, experiment with different values, and let Python be your guide. Join me tomorrow as we'll learn more about If statement and loops.
Happy coding!
*** Explore | Share | Grow ***
Commenti