Post

Python Programming Examples | Part-1

Get started

Python is a powerful, versatile programming language that is widely used for web development, data analysis, artificial intelligence, and more. It’s known for its simple and easy-to-learn syntax, making it a great choice for beginners.

To help you get started with Python programming, here are a few basic examples.

To run the code, type python <filename.py> in the shell.

Hello, World!

The one way to get started with any programming language is by printing Hello, World!. So let’s print it out!

1
2
3
# Program to print Hello, World!

print("Hello, World!")
1
Hello, World!

The above code is pretty self-explanatory. We use built-in print() function to print the string Hello, World!.

string - A string is a data structure in Python that represents a sequence of characters

Arithmetic Operations

There are seven arithmetic operators in Python:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Exponentiation (**)
  • Floor division (//)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Program to perform Arithmetic Operations

num1 = 15
num2 = 3

print("The sum of {0} and {1} is: {2}".format(num1, num2, num1 + num2))
print("The difference of {0} and {1} is: {2}".format(num1, num2, num1 - num2))
print("The multiplication of {0} and {1} is: {2}".format(
  num1, num2, num1 * num2))
print("The division(float) of {0} and {1} is: {2}".format(num1, num2, num1 / num2))
print("The division(floor) of {0} and {1} is: {2}".format(
  num1, num2, num1 // num2))
print("The modulus of {0} and {1} is: {2}".format(num1, num2, num1 % num2))
print("The value of {0} to the power {1} is: {2}".format(
  num1, num2, num1**num2))

1
2
3
4
5
6
7
The sum of 15 and 3 is: 18
The difference of 15 and 3 is: 12
The multiplication of 15 and 3 is: 45
The division(float) of 15 and 3 is: 5.0
The division(floor) of 15 and 3 is: 5
The modulus of 15 and 3 is: 0
The value of 15 to the power 3 is: 3375

We can observe from the above code that the output statement is in the format of str.format(*args, **kwargs). format() method can be invoked on a string that contains literal text or replacement fields separated by brackets {}. Either the name of a keyword argument or the positional argument’s numerical index is present in each replacement field. It returns a copy of the string with the string value of the relevant argument in place of each replacement field.

Index or base index starts with 0. Why not 1? Because 0-based indexing simplifies the implementation of algorithms and makes the code more efficient by allowing for direct translation between the index of an item and its memory address, and it makes it easier to implement algorithms that work with ranges of items in an array.

Swapping

Swapping can be done in multiple ways. Let’s swap two values in different methods.

#1 - Temporary variable

1
2
3
4
5
6
7
8
9
10
11
a = 999
b = 111

print("The value of a and b before swapping is {0} and {1}".format(a, b))

temp = a
a = b
b = temp

print("The value of a and b after swapping is {0} and {1}".format(a, b))

1
2
The value of a and b before swapping is 999 and 111
The value of a and b after swapping is 111 and 999

We assign the value of a to a temporary variable called temp. Then assign the value of b to a and then temp to b to swap the values.

#2 Without temporary variable - 1

1
2
3
4
5
6
7
8
9
10
11
a = 999
b = 111

print("The value of a and b before swapping is {0} and {1}".format(a, b))

a = a + b
b = a - b
a = a - b

print("The value of a and b after swapping is {0} and {1}".format(a, b))

1
2
The value of a and b before swapping is 999 and 111
The value of a and b after swapping is 111 and 999

#2 Without temporary variable - 2

why not just swap it in one line?

1
2
3
4
5
6
7
8
9
a = 999
b = 111

print("The value of a and b before swapping is {0} and {1}".format(a, b))

a, b = b, a

print("The value of a and b after swapping is {0} and {1}".format(a, b))

1
2
The value of a and b before swapping is 999 and 111
The value of a and b after swapping is 111 and 999

The expression a, b = b, a is a shorthand way to swap the values of two variables in a single line of code. The expression b, a creates a tuple containing the values of b and a in that order. Then, the tuple is unpacked on the left-hand side of the assignment statement, with the variable a getting the value that was in b, and the variable b getting the value that was in a.

#2 Without temporary variable - 3

Here’s another method to swap by using the XOR operator.

1
2
3
4
5
6
7
8
9
10
11
a = 999
b = 111

print("The value of a and b before swapping is {0} and {1}".format(a, b))

a = a ^ b
b = a ^ b
a = a ^ b

print("The value of a and b after swapping is {0} and {1}".format(a, b))

1
2
The value of a and b before swapping is 999 and 111
The value of a and b after swapping is 111 and 999

Suggested reading -Bitwise Operators in Python

The ^ operator is used to perform the bitwise exclusive or (XOR) operation. The XOR operation compares each bit of the first operand to the corresponding bit of the second operand. If the bits are the same, it returns a 0 in that position. If the bits are different, it returns a 1 in that position.

See what’s happening in the above swapping process…

1
2
3
4
5
6
7
8
9
10
11
12
XOR Swap

original values                original values
a = 999         = 999
b = 111         = 111
do the swap                do the swap
a = a xor b     = 0b0000001111100111
b = a xor b     = 0b0000001110001000
a = a xor b     = 0b01101111
final values                final values
a               = 0b01101111
b               = 0b0000001111100111

Meme on XOR operator If you understand this meme, then you are good to go with the XOR concept.

#2 Without temporary variable - 4 Can you think of swapping the numbers through multiplication and division? Give it a try!

The last one (in the above meme) comes under file handling in python, Which we will explore later. If you are curious, go check out this doc.

Bit to Byte conversion

What is a Bit? According to wiki - “The bit is the most basic unit of information in computing and digital communications.”

What is a Byte? Digital information is stored in units called bytes, which typically have eight bits.

So lets convert bits to bytes using python. In this program, we will get the input from the user to enter the number of bit. To do this we need input() function.

From the docs: The input(prompt), if the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

As mentioned above, it holds the value in string. In order to convert it into integer data type. To do this, we need to do typecasting. int() is a constructor that allows to convert different type of value to integer, it’s a built-in function and can be use to convert numbers represented as strings, floating-point numbers and other types to integers.

1
2
3
4
5
number_of_bit = int(input("Enter the total number of bit: "))

number_of_byte = number_of_bit // 8

print("{0} bit is equals to {1} byte".format(number_of_bit, number_of_byte))
1
2
Enter the total number of bit: 16
16 bit is equals to 2 byte

Meme on bit and byte Wait a minute…

Positive, Negative or Zero

You know it! Basic maths…But, we will introduce some new keywords. Keywords are the reserved words that have specific meanings and restrictions around how they should be used.

There are 35 reserved keywords in python. Those are as follows:

1
2
3
4
5
6
7
False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

The below program uses if, elif, and else keywords which are control flow tools in python.

Meme on bit and byte This meme actually explains the working of if, elif, and else.

1
2
3
4
5
6
7
8
num = float(input("Enter the number: "))

if num > 0:
  print("Positive number")
elif num == 0:
  print("Zero")
else:
  print("Negative number")
1
2
Enter the number: -12
Negative number

This is just getting started. Thank you for reading! If you enjoyed this article, please feel free to share your feedback or leave a comment below. I always appreciate hearing from my readers and am happy to answer any questions you may have. you can follow me on Twitter at @dotAadarsh. Thank you again for your support and I hope you have a great day!

This post is licensed under CC BY 4.0 by the author.