Saturday, March 7, 2015

Assign a Variable in Python - Python Basics - Python Tutorial

Assign a Variable in PythonAssign a Variable in Python


Before we can focus on Python basics we need to learn how to assign a variable in Python.  We will dive deeper into variables in a later tutorial.  This tutorial main focus is on assigning variables and calling variables after they been assigned which will make learning data types in this chapter easier.


Variable Creation


A variable in Python is created when it is assigned a value.  We assign a value to a variable with the equal sign(=).  Unlike other languages we do not need to assign a type or even declare it before a value is assigned to the variable.


Variable Syntax


Variable name must start with a letter and after first letter it contain numbers.  Variables can not start with numbers.


var = 3


var2 = “Cat”


Var3 = [123, 567, 987, 3456]

The above are examples how we create a variable by assigning it a value.


Call a Variable


To call a variable in python we simply just type the variable name and press return.  Check out the examples below for a better understanding.


Creating and Calling Variables in Python Examples


>>> a = 7
>>> a
7
>>> var = "This is our var"
>>> var
'This is our var'

This is a very simply overview of assign a variable in Python.  We will dig deeper into variables in a later tutorial.



No comments:

Post a Comment