Python Variables

Python Variables

Introduction

Variables are the containers used to store data into them. A variable can store any type of data (string, numbers, list, etc.) in it.

The best thing about Python is we do not need to define the data-type initially while creating the variable. Python is smart enough to identify itself. However, we can later modify its type as per our requirement.

Example:

  '''
  Declaring the Variables
  '''
  a = 2
  b = 'Python'
  print(a)

Now while declaring a variable in Python, there is a set of rules which we as a developer have to keep in practice to standardize our code. They are:

  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, name, and Name are three different variables).
  • A reserved Keyword should not be used as a variable name.
  • As per PEP-8 convention, variable names should be lowercase, with words separated by underscores as necessary to improve readability.
  '''
  PEP-8 Convenction for Naming
  '''
  username = "Platforuma"
  user_name = "Platforuma"   #usually preffered and will be using this
  _user_name = "Platforuma"
  userName = "Platforuma" 
  USERNAME = "Platforuma"

The most commonly used methods of constructing a multi-word variable name are the last three examples:

  • Camel Case: Second and subsequent words are capitalized, to make word boundaries easier to see. (Presumably, it struck someone at some point that the capital letters strewn throughout the variable name vaguely resemble camel humps.)
    • Example: userName
  • Pascal Case: Identical to Camel Case, except the first word is also capitalized.
    • Example: UserName
  • Snake Case: Words are separated by underscores.
    • Example: user_name

As per PEP-8’s recommendation:

  • Snake Case should be used for functions and variable names.
  • Pascal Case should be used for class names. (PEP 8 refers to this as the “CapWords” convention.)

Knowing the Data-Type

We can also know the data-type of the variable once it is created.

Example:

  '''
  Knowing the Data-Types of Declared Variables
  '''
  a = 'Python'
  print(a)
  print(type(a))

  b = 25
  print(b)
  print(type(b))

  c = 32.2
  print(c)
  print(type(c))

  d = ['a', 'b', 1, 2]
  print(d)
  print(type(d))

  e = ('a', 'b', 1, 2)
  print(e)
  print(type(e))

  f = {1, 2, 3}
  print(f)
  print(type(f))

  g = {'a': 'apple', 'b': 'ball', 'c': 'cat'}
  print(g)
  print(type(g))

Assigning Values to Variables

We can assign values to a variable in multiple manner. Some of them are

  '''
  Assigning values to variables
  '''
 #single assignment  
  a = 2
  b = 'Python'
  print(a)
  print(b)

 #multiple assignment 
  a, b = 2, 'Python'
  print(a)
  print(b)

 #similar assignment 
  a = b = c = 5
  print(a)
  print(b)
  print(c)

Input From the User

We can ask user to give us a value. Most of the real life project depends on the values which the user provide. In Python, it’s very easy to ask the user for the values and store them in a variable.

Point to Remember: Python always store the value provided by the user as a ‘STRING’. We need to later convert the data-type as per our requirement.

In the ‘INPUT’ Syntax, we can add a prompt statement in the ( ‘ ‘) so that the user can know it on the screen and then act accordingly.

To know about how to convert the data-type go to Python – Data Types

  '''
  Input From the User
  '''
  a = input('Enter the value: ')
  print(a)

There is a few more important concept regarding the variable which we will deep dive in Functions and then in OOPs concept.

Get started with Python: What is Python? | Data & Data-Types in Python

Follow us on:

Source: Platforuma

Leave a Comment

Your email address will not be published. Required fields are marked *

EXTRA ₹999 OFF on Lock-Down Live Learning!

Join Now!
X myStickymenu