Variables and Types

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.

Assigning Values to Variables

Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.

Numbers

Python supports two types of numbers - integers and floating point numbers. (It also supports complex numbers, which will not be explained in this tutorial).

To define an integer, use the following syntax:

myint = 7

To define a floating point number, you may use one of the following notations:

myfloat = 7.0
myfloat = float(7)

Strings

Strings are defined either with a single quote or a double quotes.

mystring = 'hello'
mystring = "hello"

The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes)

mystring = "Don't worry about apostrophes"

Printing the variables.

#!/usr/bin/python

balance = 100          # An integer assignment
money   = 1000.0       # A floating point
owner   = "John"       # A string

print balance
print money
print owner

or 

print owner , " had " , money ,", but after 30 minute of shopping, he is left with", balance

Concatenation in Python

Concatenation in python can only be done with similar variable type, for otherwise, you will require to typecase

one = 1
two = 2
three = one + two

hello = "hello"
world = "world"
helloworld = hello + " " + world

Multiple Assignment

Python allows you to assign a single value to several variables simultaneously. For example ?

a = b = c = 1

Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example ?

a, b, c = 1, 2, "Rocky"

Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "Rocky" is assigned to the variable c.

Standard Data Types

The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.

Python has five standard data types ?

  1. Numbers

  2. String

  3. List

  4. Tuple

  5. Dictionary

Python supports four different numerical types ?

  1. int (signed integers)

  2. long (long integers, they can also be represented in octal and hexadecimal)

  3. float (floating point real values)

  4. complex (complex numbers)

Delete Reference to a number object

You can also delete the reference to a number object by using the del statement. The syntax of the del statement is ?

del var1[,var2[,var3[....,varN]]]]

You can delete a single object or multiple objects by using the del statement. For example ?

del var
del var_a, var_b

We will learn Lists, Tuples and Dictionary in our next tutorial


Loading ...

Related Results :

  1. Variables and Types
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author