Certainly! Let's get started with some basic Python code examples that you can run in your Jupyter Notebook in Google Colab. We'll cover fundamental concepts to help you get comfortable with Python programming.
1. Hello, World!
Let's start with the classic "Hello, World!" example to print a message:
print("Hello, World!")
You can create a new code cell in your Jupyter Notebook, paste this code, and run the cell to see the output.
2. Variables and Data Types
In Python, you can create variables to store data. Python supports various data types, including integers, floats, strings, and booleans.
# Variable assignment
x = 5
y = 3.14
name = "John"
is_student = True
# Printing variables
print(x)
print(y)
print(name)
print(is_student)
3. Basic Arithmetic Operations
Python can perform arithmetic operations such as addition, subtraction, multiplication, and division:
a = 10
b = 3
# Addition
addition_result = a + b
# Subtraction
subtraction_result = a - b
# Multiplication
multiplication_result = a * b
# Division
division_result = a / b
print("Addition:", addition_result)
print("Subtraction:", subtraction_result)
print("Multiplication:", multiplication_result)
print("Division:", division_result)
4. Lists and Indexing
Python lists are collections of items. You can access list elements by their indices:
fruits = ["apple", "banana", "cherry"]
# Accessing elements by index
print(fruits[0]) # "apple"
print(fruits[1]) # "banana"
print(fruits[2]) # "cherry"
# Negative indexing
print(fruits[-1]) # "cherry"
5. Loops
Python supports different types of loops, such as for
loops and while
loops:
# For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
6. Conditional Statements
Python uses if
, elif
, and else
for conditional statements:
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
These are some basic Python code examples to get you started. Feel free to create new code cells in your Jupyter Notebook and experiment with these concepts. You can build upon these fundamentals to tackle more complex tasks and projects. If you have specific questions or need assistance with a particular coding task, please let me know! Certainly! Let's get started with some basic Python code examples that you can run in your Jupyter Notebook in Google Colab. We'll cover fundamental concepts to help you get comfortable with Python programming.
1. Hello, World!
Let's start with the classic "Hello, World!" example to print a message:
print("Hello, World!")
You can create a new code cell in your Jupyter Notebook, paste this code, and run the cell to see the output.
2. Variables and Data Types
In Python, you can create variables to store data. Python supports various data types, including integers, floats, strings, and booleans.
# Variable assignment
x = 5
y = 3.14
name = "John"
is_student = True
# Printing variables
print(x)
print(y)
print(name)
print(is_student)
3. Basic Arithmetic Operations
Python can perform arithmetic operations such as addition, subtraction, multiplication, and division:
a = 10
b = 3
# Addition
addition_result = a + b
# Subtraction
subtraction_result = a - b
# Multiplication
multiplication_result = a * b
# Division
division_result = a / b
print("Addition:", addition_result)
print("Subtraction:", subtraction_result)
print("Multiplication:", multiplication_result)
print("Division:", division_result)
4. Lists and Indexing
Python lists are collections of items. You can access list elements by their indices:
fruits = ["apple", "banana", "cherry"]
# Accessing elements by index
print(fruits[0]) # "apple"
print(fruits[1]) # "banana"
print(fruits[2]) # "cherry"
# Negative indexing
print(fruits[-1]) # "cherry"
5. Loops
Python supports different types of loops, such as for
loops and while
loops:
# For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
6. Conditional Statements
Python uses if
, elif
, and else
for conditional statements:
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
These are some basic Python code examples to get you started. Feel free to create new code cells in your Jupyter Notebook and experiment with these concepts. You can build upon these fundamentals to tackle more complex tasks and projects. If you have specific questions or need assistance with a particular coding task, please let me know!