Introducing Python

Python is the leading language in the industry. It has passed Java in terms of popularity and is growing day by day. It offers lots of libraries which can help solve technical challenges quite easily.
With AI and ML booming in the market and all the big giants like Google, Microsoft adopting it, it has become absolutely imperative to switch to Python libraries. In this article, we will share all that one should know about Python and it's syntax. It is a beginner level course and anyone can start learning it.

Recently Python released 3.7 version on October 19,2019.

One can start by downloading and installing PyCharm. PyCharm is an interpreter where you can actually test the Python coding.

How to print Hello World in Python
Believe me, it's very simple -

print("Hello World")

That's it!!!! Easy? Isn't it?

Variables in Python -
As you already know, variables are the memory location where we store the data. To assign the value in a variable Equal operator is used. For example,

age = 20
print(age)
Will print '20' as an output. There is a concept of multiple assignments in Python. For example, consider below code -

Mike, Skyler, Jesse = 18,20,21
Will give output as defined above. Another declaration can be re-written as below -

Mike=Skyler=Jesse=18
This means that the age of Mike, Skyler, and Jesse is 18 and is declared at once using Python syntax of multiple assignments.

Arithmetic operators in Python
Just like any other programming language, arithmetic operators in Python can be defined as follows -

age1=12
age2=18
age1 + age2
 age1 - age2
 age1 * age2
age1 / age2
age1 % age2 
 age2 % age1
The output for the above expressions will be 30, -6, 216, 0.66,  12, 6.

Let's suppose there is a String.

Sentence = "This is a String" 
Sentence[0]
Sentence[0:4]
Sentence[:3]
Sentence[:-2] 
The output for the above commands will be "T", "This", "Thi" and "This is a Stri".

Placeholders in Python
Placeholders are defined by the symbol '%'. For example.
Sentence = "%s is 21 years old"
name = "Jerry" 
Sentence%name
Sentence%["Chandler"]
Sentence2 = "%s %s is Prime Minister of India"
Sentence2%["Narendra","Modi"] 
The above commands will print "Jerry is 21 years old" and "Chandler is 21 years old", "Narender Modi is Prime Minister of India". That's where placeholder comes into play. %s is used for String, %d is used for integer.

Lists in Python
The list is a set of items that are ordered and have some indexes. Instead of creating tens or hundreds of variables, a list can come in handy. A list in Python can be declared as follows-
Fruits = ["Watermelon","Orange","Cherry",Äpple"]
Fruits[0]
 Fruits[0] will give output as "Watermelon". A lot of other methods are associated with the lists such as append, del, len and Max/Min.

Fruits.append["Blueberry"] -> ["Watermelon", "Orange", "Cherry", Äpple", "Blueberry"]
 del Fruits[0] -> [Örange", "Cherry", Äpple", "Blueberry"]
len(Fruits) -> 4
 age =[21,28,43,35]
Max(age) = 43 
Arithmetic operators can be applied to lists. Two lists can be merged using the addition operator and one list can be multiplied several times. I think that's all one needs to know when it comes to Lists in Python.

Dictionaries in Python
What if you want to include String as well as integers in a list?  Such that you can pull value with the help of Key? Well, that's possible through Dictionaries in Python. Consider an example below where

Employee= {"James": 29 ,  "Mike":32 ,  Ëllie": 27}
 Employee["James"] -> 29
Please note that dictionaries are declared in parenthesis and key-value pairs.  del and len can be used in the dictionary. The key-value pair should always be unique.

Tuples in Python
A tuple is another data structure in Python. Tuple items cannot be deleted or updated, they are immutable. A Tuple can be declared as -
tup = ("Watermelon", "Orange, "Banana")
You cannot delete a specific element from Tuple. A tuple is declared in (). Del and len apply in Tuple as well.

Conditional Statements in Python
Let's start with if statement first -
if(5>3):
        print("Hello")
          else:
                 print("Not true")
Make sure if and else conditions are properly indented. Python follows indentation rigorously for better readability of the code.

elif statement -
age = 16
if(age<16):
       print(Ãœnder 16")
elif(age>=16 and age<25):
      print(Ãœnder 24");
else(age >=25)
     print(Öld guy")
I hope the above statements are clear if you are into programming.

For Loop
list1 = ["Jerry", "Berry", "Carry"]
for item in list1:
      print(item)
The output will be -
Jerry
Berry
Carry 
We can also use the Range function, for example -
for i in range(1,10):
      print(i) 
It will print all the consecutive numbers starting from 1 up to 9. We can also skip the numbers if we use below for loop -

for i in range(1,11,2)
print(i)
It will print -
1
3
5
7
9

Nested For loop
for i in range(0,5):
       for j in range(0,3):
print(i*j)
The above code will print -
0
0
0
0
0
1
2
0
2
4
0
3
6
0
4
8

While loop in Python
While loop is similar to that of any other programming language. The statement will be executed until the conditions are met. There are 3 control statements - Break, Continue and Pass which can be used based upon the flow.

Try and Except in Python
Try and except code is like an if-else loop. Please find the syntax below
try:
if(name>3):
print("Hello")
except:
print("There is something wrong")

The Above code will print "There is something wrong".

Comments in Python
Single line comment comes after # in Python whereas multiline comments come between """.

Functions in Python
Users can define the functions in Python using keyword def. For example ;

def HelloWorld():
print("This is a function")
HelloWorld()
One needs to call the function to reuse the same code over and again. There are no data types used in python, please remember not to pass the datatypes un argument.


        
  




Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Coforge Interview Questions | Automation Testing profile

Testing in CI/CD