Exploring New Techs!

Getting Started with Python


Hello Technotizers, in the previous article we had gone a through a detailed introduction to Python. Visit the link if you had missed out the article: Introduction to python

Python is an interpreted, high level, general-purpose programming language which is very famous for its simple programming syntax. In this article we are going to get started with Python. We will learn the Basic syntax, keywords in Python, Python Identifiers, Python Indentation and Python comments.


  • Jupyter Notebook:  The development environment that is used in this article is Jupyter Notebook. The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more. It can be installed on your device and can also be used directly on the browser. Visit the link to know more and installhttps://jupyter.org/    

  •  First program: In the very first step let’s create the program which prints “Hello World”. The keyword ‘print’ is used for printing statements in Python. Since it's a very simple program, it's often used to introduce a new programming language to beginners.

    print(“Hello World”)                 

Type the above code in the notebook and see the output as “Hello World”. Wuhu, you’ve created your first code!!


  • Keywords in Python: Keywords are reserved words in Python. These cannot be used as a variable name, function name or as an identifier. Keywords are used to define the syntax and structure of the Python code. In Python, keywords are case sensitive and there are 33 keywords in Python 3.7. All the keywords leaving TrueFalse and None are in lowercase and they must be written as they are. The list of all the keywords is given below.

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

 

  •  Python Identifiers:


An identifier is a name given to class, functions, variables, etc.

Rules for writing identifiers:

1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _. Examples: myClassvar_1 and print_this_to_screen

2. An identifier cannot start with a digit. 1name is invalid, but name1 is a valid name.

3. Keywords cannot be used as identifiers.

4. Special symbols like !@#$% etc. cannot be used in identifiers.

5. An identifier can be of any length.


Ø      Note: Python is a case-sensitive language. This means, Name and name are not the same. Identifiers should be named in a way that makes sense. While c = 10 is a valid name, writing count = 10 would make more sense, and it is easy to figure out what it represents. Multiple words can be separated using an underscore rather than a space, like this_is_a_long_variable.

Let’s now declare a variable and use it in the code:

   my_name="Harsh"                        
  print("My name is:",my_name)            
  my_age=20                               
  print("My age is:",my_age)
              

 

The output of the above code is given below:

   My name is: Harsh                      
   My age is: 20                          

 

  • Python Indentation:

Many other programming languages like C, C++, Java use braces {} for representing a code block (body of a function or loop). But, Python uses Indentation. Generally four whitespaces are used as indentation. The usage of indentation in Python makes the code look neat and clean. Indentation can be ignored in line continuation, but it's always a good idea to indent as it makes the code more readable. An example of Indentation is shown below:

    for i in range(1,10):                  
        print(i)                           
        if i==7:                           
            break                          

 

  • Python Comments:

Comments are very important while writing a code as it specifies what is going inside the code. The coder as well as the visitor can understand the code easily by reading the comments. Sometimes we may forget the key details of the code or function, writing comments are very useful at such times. Also these comments are completely ignored by the compiler and they are only added for feasibility purpose. # symbol is used to start the writing of a comment. Below is the example.

  #this is a comment.                    
  #print out your name below             
  print("I am a Technotizer!")           

         

With this we come to the end of the article. Hope it was helpful. Don’t miss the next article as we will be including important Python data structures into it. See you then!!

Keep coding and exploring new techs!

0 comments:

Post a Comment