Understanding variables in Python (Part 1)

Understanding variables in Python (Part 1)

Hello world !!

I’ve been learning basic python syntax for some months now. So I decided to complete the last phase of learning by writing about variables in python.

By the end of this article you must have understood

  • What a variable is
  • How to assign a variable in python
  • Re- assigning a variable
  • Naming conventions
  • Data types of variables
  • The types of variables in python by scope
  • Rules for naming variables

What is a variable

A variable is a container for holding data in a computer program. It keeps a value(which can be input by the user or the program data/argument) in a program. The value of the variable can be changed anytime in the program.

How to assign a variable in python.

The variables are most times declared before assigning it in some languages. But in python there is no need for an explicit declaration i.e. initializing a variable with the data type before assigning value to the variable. Like saying I want to buy a bag before going to actually buy it, but in python you just buy the bag without having to say it. In python you assign a value to a variable without declaring it. We use the ‘equal to’ sign ( = ) as the assignment operator.

Example:

 name =  “Ada”
          print (name)  # to display the variable

In python you assign a value to a variable name. i.e. the name of the variable goes on the left, with the assignment operator in the middle and the value you want to store in the variable on the right.

Example:

variable_name = value
student = “Ada”  #student is the variable name while Ada is the value

And not

  “Ada” = name

Note: In python there is no need for punctuation at the end of the assignment statement as it is the case in most programming languages.

Re- declaring a variable

When a variable name has two different values assigned to it.

 y = 7
 y = 100
 print (y)

The last value assignment is regarded as the most current, hence the last assignment will be printed as in the case above, 100 will be printed.

Naming conventions

In python there must be no whitespace between a variable name. When writing a variable name that consists of two words we have conventions for writing them. These conventions are:

camelCase convention

When writing the variable name the first letter starts with a small letter the rest starts with Capital without a space in between them.

 firstWithdrawalMonth = january 
or 
firstMonth = january

Underscore_convention

When writing the variable name, an underscore is used to separate two words

first_student = Ada
or
first_withdrawal = january

Data types of variables

Data types deal with what is stored and not how it is stored. There are different data types in python for the purpose of this article I will just highlight three (3) which are integer, float and string.

Integer (int) is a data type that consists of only numbers (positive and negative) without a decimal point. Float (float) is a data type that consists (stores) decimal numbers. String (str) is a data type that consists of only characters / alphabets. Any variable enclosed in a quotation mark is regarded as a string in python

name = “Ada” #data type is string
Age = “67#data type is string

When you assign a number to a variable and the number is enclosed in quotation marks as in above then python regards the number as a string. Since in python you don’t necessarily define the data type of a variable you can find out the data type of the variable by using the “type” keyword.

To check the data type of any variable:

name = “Ada”
print (name)
print (type(name))

Types of variables in python by scope.

In python there are two types of variable definition by scope.

  • Global variable
  • Local variable

Global variables are variables assigned outside functions and classes. They can be called anywhere within the program

x = “Hello world”
print (x)

while Local variables are variables assigned within functions and classes. They are already localised with the function or class and so you can’t call them outside their scope.

 def greeting():
          x = “Hello World”
     print (x)
greeting()

In the function above “x” is a local variable and its scope is within the function.

Note: A global variable can be assigned within a function using the global statement.

def myfunc():
          global x
          x = "Hello world"
myfunc()
print("And she said” + x)

Rules for naming variables.

While naming a variable there are some rules that must be followed:

  1. A variable name must start with a letter or the underscore character (greeting or _greeting).
  2. A variable name cannot start with a number eg: 6ft or 9_walter_street.
  3. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  4. Variable names are case-sensitive (address, Address and ADDRESS are three different variables).

If you have any more questions feel free to send me a DM on twitter . I would also love to get your feedback.