Our Support: During the COVID-19 outbreak, we request learners to CALL US for Special Discounts!

- Python Blogs -

How To Write Your First Program In Python?

Expecting you might be an outright beginner to the programming! Writing computer programs is essentially conversing with your computer and requesting that they play out specific activities. This is done through Terminal, in which you can straightforwardly compose directions to your PC, just as run programs you write in a text editor. The large issue here is that computers don't communicate in English, Spanish, or any language customarily used to convey between people. Diverse programming dialects like Python, C, Java, and so forth have been made to create complex projects, just as perform basic directions that computers can comprehend. It's not unexpected to be confounded and baffled when learning a programming language just because it expects us to move toward issues alternately. In numerous initial Computer Science courses, educators will request that students how to make a nutty spread and jam sandwich.

The students will react with straightforward directions, for example, "open the sack of bread, at that point take out two cuts, spread nutty spread on one cut, spread nutty spread on the other", and so forth. The issue here is that they're expecting the individual listening comprehends that to spread the nutty spread, you have to open the container, embed the blade, get some nutty spread, get a cut of bread, and equally spread all through. When composing code, think about your computer as something that will just do precisely what you tell it to. This takes some becoming accustomed to, yet before you know it, you'll naturally begin figuring along these lines and your mind will be set to take care of issues productively. In this blog, we’ll be talking about “Hello world” using Python by following steps as explained below:

  • Writing your first Python Program
  • Installation of Python
  • Configuring Python in different environments
  • Creating your first Python program
  • Running your first Python program
  • Adding a comment in your Python program
  • Another practice of writing a basic program in Python

Writing your first Python Program

Probably the coolest element of Python is that it is an interpreted language (in actuality, Python contents are first arranged to some bytecode, and that bytecode is translated). This implies we don't have to run a different compile step (for example making an interpretation of our program into machine code) to run our program. Indeed, we can even run the interpreter in what is known as an intelligent mode. This will enable us to try out directions for each line in turn!

To begin, we'll advise Python to print the expression, "Hello, World!" to the terminal. We'll do this first from the interpreter and afterward, we'll make a record and run it as a program. This will give both of you the primary approaches to interface with Python.

The least complex approach to make Python programs is to compose your code in a text editor (for example nano, vim, emacs, Midnight Commander, Leafpad, and so on.), save it, and afterward run it from the terminal with the command python <FILE>.py.

Some users like to utilize an integrated development environment (IDE) when creating code. IDEs offer various advantages including syntax highlighting, code finishing, a single-click running, debugging insights, and so forth. Nonetheless, most IDEs require a graphical interface to utilize, which implies you should be on the full desktop version of Raspbian.

To create and run our first Python program, we’ll use the following operating systems:-

Windows, Mac, and IDLE

IDLE is the default Python editor that has been accessible on Raspbian for some ages. Fortunately, it has a built-in interpreter, which enables you to run commands each in turn to test code. The awful news is that it doesn't show line numbers, and it just works with Python (yet you're here for Python in any case, right?).

Open IDLE by choosing the Raspberry Pi logo in the upper left, and click Programming > Python 3 (IDLE). You ought to be given the Python intuitive interpreter.

Installation of Python

Installation of Python

In Windows

  • Download the latest version of Python from the Python webpage.
  • Double-tap on the symbol of the document that you just downloaded.
  • Acknowledge the default alternatives given to you until you get to the Finish button. Your establishment is finished.

In Mac

Python comes packaged with Mac OS X. Download the most recent binary version of Python that runs on both Power PC and Intel frameworks and install it on your system.

Read: Learn Python Strings : An Expert Guide

Using IDLE

Starting IDLE on Mac

  • In a Terminal window, type python. This will begin the Python shell. The prompt for that is >>>
  • At the Python shell prompt type import idlelib.idle
  • This will start the IDLE IDE
  • Using IDLE on either Windows or Mac

Start IDLE

  • Go to the File menu and click on New Window
  • Type your program in
  • Go to the File menu and click on Save. Type in filename.py This will save it as a plain text file, which can be opened in any editor you choose (like Notepad or TextEdit).
  • To run your program go to Run and click Run Module

Configuring Python in different environments

In Windows

  • Starting at My Computer go to the accompanying directory C:\Python27. In that folder, you should see all the Python documents.
  • Copy that address beginning with C: and consummation with 27 and close that window.
  • Click on Start and then Right Click on My Computer.
  • Click on Properties then Click on Advanced System Settings or
  • Click on Environment Variables.
  • Under System Variables look for the variable Path.
  • Select Path by tapping on it. Click on Edit.
  • Look over the right to one side of the field called Variable value utilizing the right arrow key.
  • Add a semicolon (;) at the end and paste the path (to the Python folder) that you recently replicated. Click OK.

Using IDLE either on Windows or Mac

  • Start IDLE
  • Go to the File menu and click on New Window
  • Type your program in
  • Go to the File menu and click on Save. Type in py This will spare it as a plain book document, which can be opened in any editor you pick (like Notepad or TextEdit).
  • To run your program go to Run and click on Run Module

Creating your first Python program

In Windows

  • Make a folder called MyPythonPrograms on your C:\ drive. You will store all your Python programs in this envelope.
  • Go to Start and either type Run in the Start Search box at the bottom or click on Run.
  • Type in notepad in the field called Open.
  • In Notepad type the following program and precisely compose:

# File: Hello.py

print "Hello World!"

  • Go to File and tap on Save as.
  • In the field Save in browser for the C: drive and afterward select the folder named MyPythonPrograms.
  • For the field File name expel everything that is there and type in py.
  • In the field Save as type select All Files
  • Click on Save.

Congrats! You have recently made your first Python program in Windows OS.

In Mac

  • Click on File and afterward New Finder
  • Click on Documents.
  • Click on File and afterward New Folder.
  • Call the folder MyPythonPrograms. You will store all class related projects there.
  • Click on Applications and afterward
  • Click on TextEdit on the menu bar and choose Preferences.
  • Select Plain Text.
  • In the void TextEdit window type the following program precisely as given:

# File: Hello.py

print "Hi World!"

  • From the File menu in TextEdit click on Save As.
  • In the field Save As: type py.
  • Select Documents and the file folder MyPythonPrograms.
  • Click Save.

Running your first Python program

In Windows

  • Go to Start and tap on Run.
  • Type cmd in the Open field and snap OK.
  • A dull window will show up. Type cd C:\ and hit the Enter key.
  • If you type dir you will get a posting of all organizers in your C: drive. You should see the folder MyPythonPrograms that you have made.
  • Type cd MyPythonPrograms and hit Enter. It should take you to the MyPythonPrograms folder.
  • Type dir and you should see the record py.
  • To run the program, type python Hello.py and hit Enter.
  • You should see the line Hello World!

Congrats, you have run your first Python program in Windows OS.

In Mac

Read: How to Perform Data Wrangling in Python?
  • Select Applications, at that point Utilities and Terminal.
  • In your Terminal window type ls and Return. It should give a posting of all the top-level folders. You should see the Documents
  • Type cd Documents and hit Return.
  • Type ls and hit Return and you should see the folder MyPythonPrograms.
  • Type cd MyPythonPrograms and hit Return.
  • Type ls and hit return and you should see the document py.
  • To run the program, type python Hello.py and hit Return.
  • You should see the line Hello World!

Congrats, you have run your first Python program in Mac OS.

Adding a comment in your Python program

Comments are a significant piece of programming. They permit you to clarify what each piece of your code does. This is particularly significant when your program is more than one line.

Yet, it's a great practice to include a comment as you compose the program, even while the program is little. It doesn't take time before your program traverses hundreds (or even a large number of) lines. And afterward, it may be a headache returning and attempting to make sense of what you were attempting to do back when you wrote the program.

In Python, comments start with the hash character (#) that isn't a piece of a string literal and finishes toward the finish of the physical line. Or if you need to compose a comment, start the line with #. Like this:

# This is a comment
print("Hello World")
RESULT:
Hello World


As you have seen above, the comment is not the output to the screen. It's essentially there for the programmer (and some other developers) advantage only.

Attempt to make your comments illustrative yet concise. Likewise, numerous software engineers and development teams utilize the act of including a huge, point by point comment at the highest point of the file that clarifies what the document does, the date it was made, its version, the creator, and so forth.

In Short,

Algorithm for your first Python program is:

  1. Start
  2. Ask the required name from the user , by printing "Enter your name:"
  3. Store this name to a variable 'n'
  4. Print HELLO NAME
  5. Stop

Program:

n= raw_input ("Enter your name: ")
print "HELLO", n

Another practice of writing a basic program in Python

Write a program that prints out your name and trusts that the client will press the Enter key before the program closes. (Seems cool, right!)

Let us begin writing some Python code for this. This program requires the use of input() function. If you want to learn more functions used in Python, you can enroll for Python online training at JanBask.

The input() function

We will find out about the input() function. The input() function is like the print() function since it yields to the Terminal anything you write in the middle of the brackets and statements. However, it requires client input (composing something in and pressing enter) to push ahead with the program or end the program if it's the last line. Below is the example:

Program with input() function

Read: Python Conditional Statements : If, Else, Elif, Nested If & Switch Case

Program with input() function

Hello World Program indicating input() function

Hello World program in the wake of pressing enter

As should be obvious, this program didn't end until you squeezed enter. The principal picture shows the screen before we pressed enter and the subsequent picture shows it after.

Try it yourself!

# Name

# Program prints name and requires user input to end

print("My name is (typeyournamehere).")
input("Press the enter key to exit.")

Conclusion

Through the above write-up, you have learned writing your first Python program using Windows, Macintosh, and IDLE. You are now able to create your basic program. Keep learning about programming in Python by enrolling in full Python training with us. Happy coding!




    Janbask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


Comments

Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

2 days 14 Jul 2020

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

19 days 31 Jul 2020

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

4 days 16 Jul 2020

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

5 days 17 Jul 2020

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

3 days 15 Jul 2020

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

12 days 24 Jul 2020

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

2 days 14 Jul 2020

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

3 days 15 Jul 2020

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation

Upcoming Class

11 days 23 Jul 2020

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks

Upcoming Class

2 days 14 Jul 2020

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning

Upcoming Class

5 days 17 Jul 2020

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop

Upcoming Class

1 day 13 Jul 2020

Search Posts

Reset

Receive Latest Materials and Offers on Python Course

Interviews