General components of python programming.

Contents of this blog:

  • Variables
  • Data types
  • Print statement
  • Type conversion
  • Input statement

Variables are named memory locations containing value. Variables can store values of different types.
You can assign a value to a variable using a equal sign(=). Eg:- a=10. If you assign a new value to the same variable, previous value will be erased and the variable will contain the new value.
In python, you don’t need to declare the data-type during assigning the variable.
Different Data-types:-

Data-types Example
Numbers- int
float
a=1058
x=0.5
String- strnm=”name”
Boolean- boolBoolean can
only have true
or false
Listc=[1,3,4]
Tupled=(1,2,5,6)

Print statement:-
Syntax:-
print(variable1,variable2,variable3)

Example:-

a=3
b=5.9
c="python" #its string. Anything within "" is string.
d=[1,2,3]
g,h,k,l=3,4.7,10,"pythonics" #you can do multiple assignments by the method shown
print(" the output is"," a=",a," b=",b)
print(c,d)
print(g," ",h," ",l)

Output:-

Type conversion:- Data types of the variables can be converted in the following ways:
Syntax:-
v2=datatype(v1)

Example:-

a=2.7
b=int(a) #float to integer
print(" b=",b)
c=3
d=float(c) #integer to float
e=str(a) #float to string
print(" d=",d," e=",e)

Output:-

Here the 2.7(value of e) is a string and not a number.

Input statement:- This allows user to input while the program is running.

Syntax:-

var=datatype(input(“enter”))

If you don’t specify the data-type, the value which user inputs is stored as a string ,so you can’t perform any arithmetic operation with it.

Example:

a=int(input("enter number "))
print(a)
b=float(input("enter number2 "))
print(b)

Output:

Leave a comment

Design a site like this with WordPress.com
Get started