Welcome to Techbbas Log!👇
First Blog: A Journey Through Basic Python Programming
Hello, fellow tech enthusiasts! Welcome to the very first blog post on Techbbas Log. Today, we are going to embark on a journey through some fundamental Python programming concepts. Whether you are a beginner or looking to refresh your knowledge, this blog will guide you through basic operations, working with lists, and manipulating data structures. Let's dive in!
Hello, World!
Let's start with the classic "Hello, World!" program, which is a simple way to ensure your Python environment is set up correctly.
```python
print("hello world")
```
Working with Variables
In Python, you can easily assign values to variables and perform arithmetic operations. Here are some examples:
```python
Assigning a number to a variable
name = 150
Printing the variable
print(name)
Adding a number to a variable
print(name + 50)
Assigning values to variables and adding them
a = 10
b = 30
print(a + b)
```
Data Types and Operations
Python allows you to work with different data types such as integers, strings, and floats. Let's see how:
```python
# Assigning string values to variables
a = "23"
b = "30"
Adding integers
print(2 + 2)
Performing division
a = 12 / 3
print(a)
Adding an integer and a float
num1 = 10
num2 = 1.5
sum = num1 + num2
print(sum)
```
User Input
You can also take input from users:
```python
name = int(input("Enter the number: "))
print(name)
```
Lists in Python
Lists are versatile data structures in Python that allow you to store collections of items.
Creating and Printing a List
```python
fruits = ["amrod", "apple", "mango", "orange", "happy"]
print(fruits)
```
Looping Through a List
```python
for fruit in fruits:
print(fruit)
```
Accessing Elements by Index
```python
print(fruits[0])
print(fruits[-2])
```
Mixed Data Types in a List
```python
mixed_list = ["apple", 1, "banana", 2, 3, "mango"]
print(mixed_list)
for mixed in mixed_list:
print(mixed)
```
Modifying Elements in a List
```python
mixed_list[0] = "blueberry"
print(mixed_list)
mixed_list[3] = "blueberry"
print(mixed_list)
```
Adding Elements to a List
```python
mixed_list.append("peach")
print(mixed_list)
mixed_list.insert(0, "peach")
print(mixed_list)
mixed_list.insert(1, "avocado")
print(mixed_list)
```
Removing Elements from a List
```python
fruits.remove("apple")
print(fruits)
fruits.insert(1, "apple")
print(fruits)
fruits_two = fruits.pop(1)
print(fruits)
print(fruits_two)
```
Slicing a List
```python
sublist = fruits[2:5]
print(fruits)
print(sublist)
```
List of Dictionaries
Creating a list of dictionaries allows you to store complex data structures.
```python
students = [
{"name": "Alice", "age": 21, "major": "Physics"},
{"name": "Bob", "age": 22, "major": "Mathematics"},
{"name": "Charlie", "age": 20, "major": "Computer Science"}
]
print(students)
Adding a new dictionary to the list
new_student = {"name": "David", "age": 23, "major": "Biology"}
students.append(new_student)
print(students)
Looping through the list of dictionaries
for student in students:
print(f"Age: {student['age']}, Major: {student['major']}")
```
Nested Lists
You can create and manipulate nested lists for more complex data structures.
```python
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[3, 4, 5]
]
Accessing elements in a nested list
element = matrix[3][2]
print(element)
Looping through nested lists
for row in matrix:
print("This will print each row of the matrix:")
print(row)
```
Conclusion
This concludes our first blog post on basic Python programming. We covered essential concepts such as printing output, working with variables, manipulating lists, and creating complex data structures. Stay tuned for more in-depth tutorials and advanced topics. Happy coding!
Comments
Post a Comment