• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Variables in Python

Variables in Python

Author: PFB Staff Writer
Last Updated: May 25, 2020

Variables

You can use any letter, the special characters “_” and every number provided
you do not start with it.

White spaces and signs with special meanings in Python, as “+” and “-” are
not allowed.

I usually use lowercase with words separated by underscores as necessary to
improve readability.

Remember that variable names are case sensitive.

Python is dynamically typed, which means that you don’t have to declare what
type each variable is.

In Python, variables are a storage placeholder for texts and numbers.

It must have a name so that you are able to find it again.

The variable is always assigned with the equal sign, followed by the value of the
variable.

There are some reserved words for Python and can not be used as variable name.

The variables are being referred in the program to get the value of it.

The value of the variable can be changed later on.

Store the value 10 in a variable named foo

foo = 10

Store the value of foo+10 in a variable named bar

bar = foo + 10

List of some different variable types


x = 123 			# integer
x = 123L			# long integer
x = 3.14 			# double float
x = "hello" 			# string
x = [0,1,2] 			# list
x = (0,1,2) 			# tuple
x = open(‘hello.py’, ‘r’) 	# file

You can also assign a single value to several variables simultaneously multiple 
assignments.

Variable a,b and c are assigned to the same memory location,with the value of 1
a = b = c = 1	

Example

length = 1.10
width  = 2.20
area   = length * width
print "The area is: " , area

This will print out: The area is:  2.42
More Reading

http://python.org/

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us