Saturday, December 31, 2022

Lesson 2: Data types in Python

 In this lesson, we will learn about the different data types in Python and how to work with them.

In Python, there are several built-in data types that you can use to store data. These data types include integers, floats, strings, and booleans.

Integers

An integer is a whole number that can be positive, negative, or zero. In Python, you can use the int data type to store integers.

Here is an example of how to create an integer variable:

age = 30

In this example, the age variable is an integer with a value of 30.

Floats

A float is a number with a decimal point. In Python, you can use the float data type to store floats.

Here is an example of how to create a float variable:

price = 10.99

In this example, the price variable is a float with a value of 10.99.

Strings

A string is a sequence of characters. In Python, you can use the str data type to store strings.

Here is an example of how to create a string variable:

name = 'John'

In this example, the name variable is a string with a value of 'John'.

Strings can also be defined using triple quotes (either single or double). This allows you to create multi-line strings or strings that contain quotes within them.

Here is an example of how to create a multi-line string:

description = ''' This is a multi-line string that can span multiple lines. '''

And here is an example of how to create a string that contains quotes:

quote = "He said, 'Hello, World!'"

Booleans

A boolean is a data type that can only have two values: True or False. In Python, you can use the bool data type to store booleans.

Here is an example of how to create a boolean variable:

is_active = True

In this example, the is_active variable is a boolean with a value of True.

Converting data types

Sometimes, you may need to convert a variable from one data type to another. In Python, you can use the int(), float(), and str() functions to convert a variable to an integer, float, or string, respectively.

Here is an example of how to convert a variable to a different data type:

age = 30 age_str = str(age) # age_str is now a string with a value of '30' price = 10.99 price_int = int(price) # price_int is now an integer with a value of 10

Working with strings

In Python, you can use the + operator to concatenate (join) two strings together.

Here is an example of how to concatenate two strings:

first_name = 'John' last_name = 'Doe' full_name = first_name + ' ' + last_name # full_name is now 'John Doe'

You can also use the * operator to repeat a string a certain number of times.

Here is an example of how to repeat a string:

message = 'Hello, World! ' print(message * 3) # prints 'Hello, World! Hello, World! Hello, World! '

There are many other string methods and operations that you can perform in Python. For a complete reference, you can refer to the official Python documentation.

Getting started with Python

 In this lesson, we will learn the basics of Python and how to get started with writing and running Python programs.

First, let's start by learning how to print a message to the console. In Python, we can use the print() function to print a message to the console.

Here is an example of how to use the print() function:

print('Hello, World!')

This will print the message 'Hello, World!' to the console.

Next, let's learn how to store data in variables. In Python, you can use variables to store data and give the data a name. You can use variables to store numbers, strings, and other data types.

Here is an example of how to create a variable and assign it a value:

name = 'John' age = 30

In this example, we have created two variables: name and age. The name variable is a string (a sequence of characters), and the age variable is an integer (a whole number).

You can also use variables in your print() statements to print the value of the variables.

Here is an example of how to use variables in a print() statement:

print('My name is', name, 'and I am', age, 'years old.')

This will print the message 'My name is John and I am 30 years old.' to the console.

That's it for this lesson! In the next lesson, we will learn about the different data types in Python and how to work with them.

What is List datatype in Python

 A list is a collection of items that are ordered and changeable. Lists are written with square brackets, and the items are separated by commas. Lists are one of the most commonly used data types in Python and are very versatile.

Here is an example of how to create a list in Python:

fruits = ['apple', 'banana', 'orange', 'mango']

You can access the items in a list by referring to the index number of the item. The index numbers start at 0, so the first item in the list has an index of 0, the second item has an index of 1, and so on.

Here is an example of how to access an item in a list:

print(fruits[0]) # prints 'apple'

You can also use negative index numbers to access items in the list, starting from the end. The last item in the list has an index of -1, the second to last item has an index of -2, and so on.

Here is an example of how to access the last item in a list using a negative index:

print(fruits[-1]) # prints 'mango'

You can modify an item in a list by assigning a new value to it.

Here is an example of how to change the second item in a list:

fruits[1] = 'strawberry'

You can also add new items to a list by using the append() method. This will add the new item to the end of the list.

Here is an example of how to add a new item to a list:

fruits.append('kiwi')

You can remove an item from a list using the remove() method. This will remove the first occurrence of the item from the list.

Here is an example of how to remove an item from a list:

fruits.remove('banana')

There are many other methods and operations that you can perform on lists in Python. For a complete reference, you can refer to the official Python documentation.

I hope this helps! Let me know if you have any questions.

Sunday, December 18, 2022

Let’s learn about the Socket programming using python

Recently one of my project required me to recreate the master slave mechanism. This is kind of similar to the one that we have in Jenkins architecture

Below are the requirements that I need to satisfy

  • The master should support multiple agents both windows and linux
  • The slave agents should constantly be connected to the Master
  • The master should be able to keep tabs on the slaves and see what each one is doing
  • The master should be able to assign tasks to each of them
  • The slave agents should share the task status frequently with the master