Python Technology Tutorial

Python: Variables

My name is Donald Trump! Well, who assigns this to you ? Yes, someone. In programming, we have to assign values to something we like to call variables. Read to know more :)

The word variable in itself speaks dynamism. It’s value, quantity, and essence, can change from time to time. In programming as well, your value changes from time to time.

In Python, variables are used to store data, like string, numbers, date-time etc. When you create a variable, a little portion is reserved within your computer memory to store this value of the variable. The value of a variable has to be assigned by you.

Types of variables

Python has five standard data types:

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Creating a variable

In Python, a variable does not need to be declared while creating or before adding a value to it. Python variables are usually dynamically typed, that is, the type of the variable is interpreted during run-time and you don’t need to specify the type of a variable.

The following program shows how to use string and integer type variables:

Example

x = 10 # Integer variable
y = “Hello World” # String variable
# Displaying variables value
print(x)
print(y)

Output

10
Hello World

Checking the type of a variable

With the method type(), we can know the type of variable we have created. It could be int, str, is used to get the type of a created variable.

Example

var1 = ‘Hello World’
print(type(var1))

Output

<class ‘str’>

Important Rules for Variables

  • Variables cannot start with a number, but can be Alpha-Numeric. Additionally, they also support underscores – hence the total range is from A-Z, a-z, 0-9, and _.
  • Variables are strictly case-sensitive, thus You and you are different variables

Data Type Conversion

Sometimes, due to functional reasons, we need to convert variables. For this purpose, python is quite straight forward. To convert between types, you simply use the type name as a function. A common industry jargon is ‘Type Casting‘.

There are several built-in functions to perform conversion from one data type to another. These functions return a new object, with the changed data type 🙂.

Sr.No.Function & Description
1int(x ): Converts x to an integer.
2long(x): Converts x to a long integer.
3float(x): Converts x to a floating-point number.
4str(x): Converts object x to a string representation.
5tuple(s): Converts s to a tuple.
6list(s): Converts s to a list.
7set(s): Converts s to a set.
8dict(d): Creates a dictionary. d must be a sequence of (key,value) tuples.
9chr(x): Converts an integer to a character.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: