Skip to main content

A Journey Through Basic Python Programming

 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

Popular posts from this blog

React Architecture

  React Architecture: React follows a component-based architecture. The main concepts are: Components: Reusable UI elements Props: Data passed to components State: Internal component data JSX: Syntax extension for JavaScript to describe UI Virtual DOM: In-memory representation of the actual DOM for efficient updates File System in a typical Create React App project: Let's break down each section: node_modules/: Contains all the dependencies installed via npm Managed by npm, you shouldn't modify this directory manually public/: Contains static assets that are publicly accessible index.html: The main HTML file where your React app is mounted favicon.ico: The icon shown in the browser tab manifest.json: Web app manifest for PWA (Progressive Web App) functionality src/: Contains the source code of your React application components/: Directory for reusable React components App.js: The root component of your application App.css: Styles for the App component index.js: The entry point ...

Step-by-Step Guide to Writing Python Programs for List and String Processing

 Python is a versatile programming language that's perfect for beginners and experts alike. In this blog post, we'll explore some fundamental Python programs that involve processing lists and strings through functions. We'll cover the following tasks: Summing elements in a list. Converting a string to uppercase. Filtering even numbers from a list. Finding the maximum value in a list. Sorting a list in ascending order. Let's dive into each task with detailed explanations and example code. 1. Summing Elements in a List Problem Statement Write a function that takes a list as an input and returns the sum of all its elements. In the main program, take the list as input from the user, call the function, and pass the list as an argument to the function. Solution We'll create a function sumOfElements that iterates over each element in the list, adds them up, and returns the total sum. In the main program, we'll take user input, convert it to a list of integers, and pas...

Python List Operations: A Comprehensive Guide

Introduction  In this blog, we will explore various list operations in Python. We will cover how to double each element in a list, remove duplicate elements, find the sum of all even numbers, replace negative numbers with zero, reverse a list, extract strings, and create a new list with the square of each element. Each task will be demonstrated using both a for loop and list comprehension. Table of Contents Doubling Each Element in a List Removing Duplicate Elements Sum of All Even Numbers Replacing Negative Numbers with Zero Reversing a List Extracting Strings from a List Creating a List with the Square of Each Element Doubling Each Element in a List Using a For Loop Using List Comprehension Removing Duplicate Elements Using a For Loop Sum of All Even Numbers Using a For Loop Using List Comprehension and the sum Function Replacing Negative Numbers with Zero Using a For Loop Using List Comprehension Reversing a List Using a For Loop Using List Slicing Extracting Strings from a Lis...