Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
Variables are named locations which are used to store references to the object stored in memory. The names we choose for variables and functions are commonly known as Identifiers. In python Identifiers must obey the following rules.
- All identifiers must start with letter or underscore ( _ ) , you can’t use digits. For e.gmy_var is valid identifier while 1digit is not.
- Identifiers can contain letters, digits and underscores ( _ ).
- They can be of any length.
- Identifier can’t be a keyword (keywords are reserved words that Python uses for special purpose).Following are Keywords in python 3.
1234567False class finally is returnNone continue for lambda tryTrue def from nonlocal whileand del global not withas elif if or yieldpass else import assertbreak except in raise
Assigning Values to Variables
Values are basic things that programs works with. For e.g 1 , 11 , 3.14 , "hello" are all values. In programming terminology they are also commonly known as literals. Literals can be of different type for e.g 1 , 11 are of type int , 3.14 is float and "hello" is string . Remember in python everything is object even basic data types like int, float, string, we will elaborate more on this in later chapters.
In python you don’t need to declare types of variable ahead of time. Interpreter automatically detects the type of the variable by the data it contains. To assign value to a variable equal sign (= ) is used. = is also known as assignment operator.
Following are some examples of variable declaration:
1
2
3
4
5
|
x
=
100
# x is integer
pi
=
3.14
# pi is float
empname
=
"python is great"
# empname is string
a
=
b
=
c
=
100
# this statement assign 100 to c, b and a.
|
Note: In the above code x stores reference to the 100 ( which is an int object ) , x don’t store 100 itself.
In Python comments are preceded by a pound sign ( # ). Comments are not programming statements that python interpreter executes while running the program. Comments are used by programmers to remind themselves how the program works. They are also used to write program documentation.
1
2
|
#display hello world
print
(
"hello world"
)
|
Simultaneous Assignments
Python allow simultaneous assignment syntax like this:
1
|
var1
,
var2
,
.
.
.
,
varn
=
exp1
,
exp2
,
.
.
.
,
expn
|
this statements tells the python to evaluate all the expression on the right and assign them to the corresponding variables on the left. Simultaneous Assignments is helpful to swap values of two variables. For e.g
1
2
3
4
|
>>>
x
=
1
>>>
y
=
2
>>>
y
,
x
=
x
,
y
# assign y value to x and x value to y
|
Expected Output:
1
2
3
4
|
>>>
print
(
x
)
2
>>>
print
(
y
)
1
|
Python Data Types
Python has 5 standard data types namely.
a) Numbers
b) String
c) List
d) Tuple
e) Dictionary
f) Boolean – In Python True and False are boolean literals. But the following values are also considered as false.
- 0 – zero , 0.0 ,
- [] – empty list , () – empty tuple , {} – empty dictionary , ”
- None
Receiving input from Console
input() function is used to receive input from the console.
Syntax: input([prompt]) -> string
input() function accepts an optional string argument called prompt and returns a string.
1
2
3
4
|
>>>
name
=
input
(
"Enter your name: "
)
>>>
Enter
your
name
:
tim
>>>
name
'tim'
|
Note that input() returns string even if you enter a number, to convert it to an integer you can use int() or eval() .
1
2
3
4
5
6
|
>>
age
=
int
(
input
(
"Enter your age: "
)
)
Enter
your
age
:
22
>>>
age
22
>>>
type
(
age
)
<
class
'int'
>
|
Importing modules
Python organizes codes using module. Python comes with many in built modules ready to use for e.g there is a math module for mathematical related functions, re module for regular expression and so on. But before you can use them you need to import them using the following syntax:
1
|
import
module_name
|
You can also import multiple module using the following syntax:
1
|
import
module_name_1
,
module_name_2
|
here is an example
1
2
3
|
>>>
import
math
>>>
math
.
pi
3.141592653589793
|
First line import all functions, classes, variables, constant in the math module. To access anything inside math module we first need to write module name followed by ( . ) and then name of class, function, constant or variable. In the above example we are accessing a constant called pi in math module
In next chapter we will cover numbers in python.