Python Keywords, Variables and Datatypes

In this tutorial, you will learn about Python keywords and data types. Also, you will learn about Python Variables.

Python Keywords
Python Identifiers
Python Variables
Python Data Types

python keywords

Python Keywords


Well, like in every other programming language, keywords are the reserved words which have predefined meaning and functionality.

Python keywords cannot be used as identifiers (variable names).

Here is the list of keywords in python.

List of Keywords in Python

False  class  finally  is  return
 None  continue  for lamda  try
 True  def  from  nonlocal  while
 and  del  global  not  with
 as  elif  if  or  yield
 pass  else  import  assert  raise
break except  in

Python Identifiers


Basically, identifiers are the unique name given to variables, functions, class and other entities in Python.

There are certain rules that need to be followed while giving a name to any entities in Python.

Rules for writing an identifier in Python


  • First letter of an identifier must be alphabet (underscore is also allowed)
  • Identifier can only contain letters, digits, and underscores
  • Identifiers in Python can be of any length
  • White space is not allowed
  • Keywords cannot be used as identifier

Python Variables


What is a Variable?

A variable is actually a memory location reserved to store some value.

In other programming languages like C, C++ variable type must be defined explicitly.

int x; //to declare an integer variable

But in Python, the values assigned to the variable determines its type. We don’t need the explicit declaration in Python to reserve memory space.

For example.

x = 2 #for an integer variable
y = 2.1 #for floating type variable
z = "Hello" #for string

Assigning value to variables

Like we mentioned in Python, you don’t need to declare types of variable explicitly. Interpreter automatically detects the type of the variable by the data it contains or it is assigned.

To assign a value to the variable, an equal sign(=) also known as assignment operator is used.

[adsense1]

For example:.

#To assign single value to an variable
x = 2 #x is an integer variable with assigned value 2
y = 2.1 #y is a floating point variable
z = "Hello" #z ia a string

#To assign multiple values simultaneously
x=y=z=2 #2 is assigned to x,y and z 
a,b=1,"Hello" #Here 1 is assigned to a and Hello is assigned to b

Python Data Types


So far we know that variable reserves a memory location to store a value and when that variable is assigned a value, it’s stored in that location.

Now, how much memory does that variable occupies depends on the type of data it is assigned to. So, the data types designate the memory required to store the variable.

Python has 5 standard data types.

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary

Python Numbers

As the name suggests, the data types which store numeric values are called Python Numbers.

Python has three numerics:

1: Integers:

  • int – Signed Integers
  • long – Long integers for representing higher values

2: float (Floating point numeric values)

3: Complex (Complex numbers)

Learn in depth about Python Numbers here.

Python Strings

Strings in Python are the sequence of characters stored in contiguous memory location i.e. array of characters. These set of characters are represented in either single quote or double quotes.

For example:

str = "Hi"

print str #prints Hi to console
print str[1] #prints first character to console
print str + "User" #concatenates the string with new string User

Output

‘+’ is used to concatenate the string in Python.

Hi
H
Hi User

Learn in-depth about Python Strings here.

Python List

List in Python is an ordered sequence of items separated by a comma(,) and enclosed within square brackets([ ]). Python list is mutable, which means that the value of items can be altered by accessing it using slicing operator [ ].

For example:

list_1 = [1,2,'a','YOLO']

Now, here is how we will use slicing operator ‘[ ]’ to access different elements of the list.

python keywords and data types - list

Python list is like an array. The difference is that array contains ordered sequence of items of similar data types, whereas list contains ordered sequence of items that can be of different data types. As shown in above code, list_1 contains items that are integers, characters, and strings.

Learn in-depth about Python List here.

Python Tuple

Python tuple is same as Python list, the only difference being that the Python tuples are immutable which mean that the items in Python tuples can be accessed but their value can’t be altered.

Another main difference between tuples and lists besides mutability is that lists are defined inside square brackets [ ], whereas tuples are defined inside parentheses ( ).

Here is one example.

python datatype tuple

Learn in-depth about Python tuples here.

Python Dictionary

A Python dictionary is an ordered list of key-value pairs. The items in the dictionary are comma separated key-value pairs.

If we have the key, we can retrieve its value anytime, however vice-versa doesn’t work. Hence Python dictionaries are used and optimized for retrieving data.

Dictionaries in Python are defined inside curly braces ‘{ }’ and to access and assign value, slicing operator  ‘[ ]’ is used.

For example.

python dictionary

Learn in-depth about Python dictionary here.

In Python dictionary, Keys are immutable and Values are mutable.