- Getting started with Python
- Python Introduction
- Python Data Types
- Python String Methods
- Python Lists, Tuples and Sets
- Python Dictionary
- Python Conditional Statements and Loops
- Python Functions
- Python OOPs
- Python Classes, Objects and Constructor
- Python Inheritance
- Python Iterators
- Python Scopes
- Python Module
- Python Try Except
- Python File Handling
Getting started with Python
Running our First Python Program
Open any text editor and save the file as demo.py and write the following code.
print("Hello World !")
To run the program from CMD open command prompt and go to the file directory and run the following command
python hello.py
OUTPUT:
Hello World !
NOTE: You just need to click on run button to run the program if you are using visual studio or any similar type of editor.
Comments in Python
For single line comment, we just need to write # at the start of the line.
For multi-line comment, the comments are written in between three double quotes like this
" " "
Multi-
Line
Comment
" " ".
Note : In Python we don't use braces instead we use Identation ( which is also mandatory ) like below
if n > 2:
print("The number is greater than 2")
elif n < 2 :
print("The number is less than 2")
else :
print("The number is 2")
#skipping the Identation will raise error
Python Variables
In python, declaring a variable is very simple.
#Integer
n = 45
#Floating Point Number
n = 45.456
#String
n = "Hello Everyone"
GLOBAL VARIABLE : These variables are created outside of a function.
LOCAL VARIABLE : These variables are created inside a function.
GLOBAL KEYWORD : To create a global variable inside a function global keyword is used.
def fun():
global x
To change the value of global variable which is outside of that function we use global keyword to do that.
x = "Hello"
def test():
global x
x="wellcome"
print(x)