Learn python in 20 minutes

Python language is very famous among the developer. It’s a scripting language which support system is very strong. Nowadays a lot of libraries build on python which it makes development so easy.

Python is a scripting language so it’s an interpreted, high-level language. It’s created by Guido van Rossum in 1991.

Now comes to how to start python –

  1. Here we are using thonny ide so download and install this ide.
  2. Write code and save filename with .py extension.
  3. Go to run button and run the script.

Hello world program in python

It’s a basic program and it’s a very important problem in python it does not only verify all environment settings are perfectly working and as well as you know how to print in python. It’s done by using a print function. But before going further we need to understand python comments; in python # used for single-line comment. Let’s see how to use comments and print function –


# Hello, world program in python
print("Welcome to python");  # value inside "" is a string value
# Output -> "Welcome to python"

# Using variables
msg = "Welcome to python";
print(msg);
# Output -> "Welcome to python"

Concatenation

Concat is a way by which we can append two string together.


# Variable concatenation
name = "Learner";
print("Welcome: " + name);
# Output --> Welcome: Learner


# Casting, num is numeric type and we are trying to append with string so we need convert numeric 2 
into string "2"
num = 2;
print("Number is " + str(num));
# Output --> Number is 2

Now come on a name; it’s a variable that can hold different data types.
In python, we have string, numeric, sequence, mapping, set, boolean and binary type of data type available apart from that we have arithmetic, assignment, comparison, identity, membership and bitwise operator available. We will cover set by set in the next phase.

If … Else

If and else represent the direction of code in witch direction code will be flow. Like


# Find the largest number among two numbers
# firstno and secondno are numeric variables which hold an only numeric value
firstno = 2;
secondno = 3;
# Now we using comparison operator
if firstno > secondno:
   print(str(firstno) + " is greater");
else:
   print(str(secondno) + " is greater");
# Output --> 3 number is greater

But what else if both numbers are equal


firstno = 2;
secondno = 2;
if firstno > secondno:
   print(str(firstno) + " is greater");
elif firstno < secondno:
   print(str(secondno) + " is greater");
else:
   print("Both number are equal");
# Output --> "Both number are equal"

Everything in python is an object that has –

  1. An identity (if)
  2. A value (mutable or immutable)

a = 5;
id(a);. # 5745746

Mutable

When we alter the item, the id still the same called mutable. List, Dictionary

Immutable

When we alter the item, the id changed called immutable. String, Integer, Tuple

List

Python doesn’t have array inbuilt but here we use a list instead of an array. So it’s a set of values, and can hold more than one value


arr = ["first", "second", "third"];

Tuple

It’s just like lists, but we can’t change their values. It’s defined using () whereas list represents using [] symbols.


W = ("sun", "mon");

Dictionary

It’s a key value pair based unordered collection


stu = {
  "name": "xyz",
  "age": "23"
}
print(stu);

# Output -> { "name": "xyz", "age": "23"}

Loop

Used iterate over all elements of set, list, tuple, dictionary and string


arr = ["first", "second", "third"];
for i in arr:
   print(i);

We can prevent iterate all elements using break keyword or stop on desire condition.


arr = ["first", "second", "third"];
for i in arr:
   print(i);
   if i == "second":
   break;

Can continue to skip over items


for num in range(0, 10):
   if num % 2 == 0:
        continue;
   else:
        print(str(num) + " is odd");

We can use range method to use index based condition


for num in range(0, 10):
   if num % 2 == 0:
        print(str(num) + " is even");
   else:
        print(str(num) + " is odd");

Function

A small block code which can return value.


def add(a, b):
   return a + b;
add(25, 25); 
# Output --> 50

Class

It’s a blue print of object.


class student:
   def __init__ (self, name): # self is necessary
         self.name = name;
   
   def getName(self):
        return self.name;
        
stu = Student("xyz");
stu.getName(); # xyz
# Output --> "xyz"

About the Author: Pankaj Bisht