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.
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:
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:
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!