Python – Variable

In python variable get declared when you assign value to variable first time

x=10;  #x will be integer
y='abcd' #y will be string
print(x)
print(y)

This will declare x as integer and y as string and you can also check data types as coded in below sample.

Find Type of Variable

you can use “type” keyword to find type of variable

Below is sample code and output

In Python variable names are case sensitive

x=10;  #x will be integer
y='abcd' #y will be string
X= 'Ten'

print( 'Value of small x=' ,x)
print('Value  of y=',y)
print('Value  of X=', X)

print( 'Type of small x=' ,type(x))
print('Type of y=',type(y))
print('Type of X=', type(X))

Output of this code will be as below

Value of small x= 10
 Value  of y= abcd
 Value  of X= Ten 
 Type of small x= <class 'int'>
 Type of y= <class 'str'>
 Type of X= <class 'str'>




Python : Basic Syntax

Today we will be covering basic syntax of python

Printing any string

python provided print function to print any string

 print("Hello, World!")

Comments in python

“#” is getting used to comment in python

#This is a comment.
print("Hello, World!")

Python interpreter also supports comment in below format with three dots

...
This iscomment
comment
...

Python Variables

In Python, variables are created when you assign a value to it:

a = 335
b = "Hello, World!"

Python Indentation

python used space to indicate block of code.

Below is example for wring indentation

Now we are correcting it and rerunning.

Before print we have added space so code of block is correctly indented

Multi-Line Statements

Python supports multiline statements with below syntax so you can have code written on multiple lines

print("multiline comment" + \
      "multiline comment" + \
      "multiline comment"
      )

Below is example

Sample code for your reference

print("Hello World")
if 5 > 2:
    print('5 is greater')

print("multiline comment" + \
      "multiline comment" + \
      "multiline comment"
      )

x= 2 + \
   3 + \
    4

print("value of " , x)

How to install Python

1 go to below URL

https://www.python.org/

For windows user you can download installed from below URL

https://www.python.org/downloads/windows/

For Ubuntu user

$ sudo apt-get update
$ sudo apt-get install python3.6

Setting path at Windows

Follow below link

Testing Python installation

Run Python and execute basic operation

We have installed python and ran basic program with it.

Create your website with WordPress.com
Get started