Getting Started

Table of Contents

Python Set up

  • Most operating systems other than Windows already have Python installed by default.
  • To install Python on Windows, go to the python.org download page and download Python. Run the Python installer and accept all the defaults.
  • When Python is installed, you can open Interpreter console and write your 1st “Hello World” program.
>>> print("Hello World!")
Hello World!
  • You can use any text editor to write your python program/script. Then save it with .py extension.
  • You can run your python program/script by typing python and the file name with .py extension on your terminal.

More on intalling python can be found here -  Install Python

 

Interactive Prompt

  • The interactive prompt runs code and echoes results as you go, but it doesn’t save your code in a file.
  • It turns out to be a great place to both experiment with the language and test program files on the fly.
  • The Python interpreter is usually installed as /usr/local/bin/python3.7 on those machines where it is available; putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:python3.7 to the shell.

  • Since the choice of the directory where the interpreter lives is an installation option, other places are possible; check with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternative location.)

  • On Windows machines, the Python installation is usually placed in C:\Python37, though you can change this when you’re running the installer. To add this directory to your path, you can type the following command into the command prompt in a DOS box:

set path=%path%;C:\python37

 

  • Open the command prompt (bash console in linux) and type python . It will show the prompt as follows with a welcome message
$ python3.7
Python 3.7 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
  • You write code after >>> and press the Enter key to get the output.
>>> print("Hello World!")
Hello World!
>>> 2+6
8

More on interactive mode will be find here - https://docs.python.org/3/tutorial/interpreter.html#interactive-mode

 

Write Your First Python Program in Script mode

Open your favorite text editor (e.g., vi, Notepad, or the IDLE editor),type the following statements into a new text file named script1.py, and save it in your working code directory. .

print(“Hello User!”)

 

Running your program with Command Line

Once You have saved the file you can run it with command window. Move to the directory in which you have saved the script. Then type python command and then the file name with .py extension to execute the scripts. You will get the following result.

C:\code> python script1.py
Hello User!

 

Comments in Python

A comment starts with a hash character (#) and ends at the end of the physical line.

In the below code, print('Hello user')  is commented. So when you run the script1.py it will not print "Hello user". Check the output

print('Hello World')
# print('Hello user')
Hello World

 

Indentation

The indentation rule may seem unusual at first glance to programmers accustomed to C-like languages, but it is a deliberate feature of Python, and it’s one of the main ways that Python almost forces programmers to produce uniform, regular, and readability.

if x:
    if y:
        statement1
else:
    statement2

Nested statements are blocked and associated by their physical indentation(without braces).

More on indentaion can be found here https://docs.python.org/3/reference/lexical_analysis.html#indentation

Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description