WayOfCoding

Python Hello world!!


Hey There!

This post will cover how to code in python using python shell or by creating a .py file i.e to write a basic program.
let's get to it then!

Once python is installed in your system, you can open a command window and type `python` to open the python shell.

83ddee01ca1a4fc6e14de879d6915b10d4a73d7de53025d1ce75467f5cea0878.png

What is a Python shell?

Python shell is nothing but an interpreter which executes instructions one by one (line by line).

In the above example:

The triple arrow you see is where you will write your code `>>>`.

Example code:python hello world


In the above screenshot you can see that I executed 3 statements:
1. `1+2` which returned the sum of 1 and 2 i.e 3. (arithmetic operation)
2. `print('Hello World')` which prints "Hello World in the console." (function call)
3. `quit()` you can see that when I type quit() >>> disappears that means you exited the python shell.

Next, let's see how to write a python program and execute it.

First, let's understand the code we are going to write.
We will create a program that will take input from the user and greet them.

  • Create a helloword.py file in a folder and write the below code.
#here we are asking the user to input the there name.
name=input("Enter your Name: ")
# next code print's a greeting line.
print("Welcome",name)
  • Save the file with extension `.py` in a directory.
  • There are two ways of running the code.
    • open cmd and traverse to the folder where `helloworld.py` program is saved. (>>>python helloworld.py)
    • open cmd anywhere and pass the .py file name with full path. (>>>python D:\helloworld.py)

Output:

output

In the above output, we can see that we provided Amit as user input to the program and the program gave the output as "Welcome Amit",

  • input() method is used to take user input.
  • The same is stored in the name variable.
  • print() method is used for printing the value.

 

Congratulation! you have written your first Python code!

 

 

Did you enjoy this article?


Amit Kumar1
Amit Kumar1

Hi All, My name is Amit, I'm a software engineering, who loves to solve programming scenarios and automate stuff using Python/Shell Scripting or any other language :P (pun of being a polyglot). Join Publish0x. https://www.publish0x.com?a=w9aAP28Pev


WayOfCoding
WayOfCoding

Let's learn together. Donate ETH here: 0x21DfffFB92B42721FC87c9b70773306023dA843B

Publish0x Publish0x

Reward the author with $0.01 in crypto, and earn yourself as you read!

20% to author / 80% to me.
Rewards are FREE. Publish0x pays them, not you.

Page not displaying correctly?