Variables, Expressions and Statements

Values, Variables and Keywords

Values, variables and keywords are the building blocks of Python programming. Thus, it is important to learn everything about it. Firstly, the Python keyword refers to unique programming terms which intend to perform some action. Further, there are 33 such keywords in Python where each serves a unique purpose. In other words, all of them build the vocabulary of Python. Moreover, they represent the syntax and structure of a Python program. Consequently, as all of them are reserved, we cannot use their names to define variables, classes or functions. Let us learn more about values, variables and keywords in this article.

values

Values

Values () refer to an inbuilt method in the Python programming language that returns a list of all the values available in dictionary.

Syntax

dictionary_name.values()

Parameters

There are no parameters

Returns

returns a list of all the values available in a given dictionary.

the values have been stored in a reversed manner.

Error

As we are not passing any parameters there is no scope for any error.

Code#1

                    

# Python3 program for illustration

# of values() method of dictionary

# numerical values

dictionary = {"deep": 2, "john": 3, "kenny": 4}

print(dictionary.values())

# string values

dictionary = {"geeks": "5", "for": "3", "Geeks": "5"}

print(dictionary.values())

Output

                    

dict_values([2, 3, 4])

dict_values(['5', '3', '5'])

Browse more Topics Under Variables, Expressions and Statements

Practical Application

Given name and salary return the total salary of all employees.

Code#2

                    

# Python3 program for illustration

# of values() method in finding total salary

# stores name and corresponding salaries

salary = {"deep" : 70000, "john" : 40000, "kenny" : 2000}

# stores the salaries only

list1 = salary.values()

print(sum(list1))  # prints the sum of all salaries

Output

Variables

A variable represents an entity whose value can change as and when required. Theoretically, it refers to a memory location that holds the actual value. Moreover, through querying the entity, we can retrieve the value from our code.

However, it needs assigning a label to that memory location so that we can reference it. Thus, we refer to it as a variable in the programming terms. We will now take a look at some of the major facts about variables in Python which will help you to make use of them efficiently:

Variables do not need declaration. Nevertheless, one must initialize them before use.

For instance:

test = 20

The above expression will result in the following actions

  • Formation of an object to represent the value 20.
  • If the variable (test) does not exist, then it will be formed
  • Association of the variable with the object, so that it can refer to the value.

The variable ‘test’ is a reference to the value  ‘20’. Take a look at the illustration below:

Example

| ~~~~~ | —– ~~~~~~~~~ ——-   ****

( test  ) —– Reference ——- ** 20 **

| ~~~~~ | —– ~~~~~~~~~ ——-   ****

Variable —– ~~~~~~~~~~ ——-  Object

Whenever the expression will change, Python links a new object (a chunk of memory) to the variable to refer to that value. Consequently, the old ones are sent to the garbage collector.

Example:

                    

>>> test = 20

>>> id(test)

1716585200

>>> test = 11

>>> id(test)

1716585232

>>>

Similarly, Python builds a cache and reuses some of the immutable objects, like small integers and strings for the purpose of optimization.

An object refers to merely a region of memory that can hold the following:

  • The actual object values.
  • A type designator for reflecting the object type.
  • The reference counter which decides when it is appropriate to reclaim the object.

Finally, it is the object which has a type, not the variable. But, a variable can hold objects of different types as and when needed.

Example:

                    

>>> test = 10

>>> type(test)

<class 'int'>

>>> test = 'techbeamers'

>>> type(test)

<class 'str'>

>>> test = {'Python', 'C', 'C++'}

>>> type(test)

<class 'set'>

>>>

Keywords

Keywords refer to the reserved words in Python. In other words, one cannot use a keyword as a variable name, function name or any other identifier. Thus, we use them for defining the syntax and the structure of the Python Language.

Moreover, keywords are cases sensitive in Python. Thus, there are 33 keywords in Python 3.7. However, this number may vary slightly over the course of time. In addition, all the keywords except True, False and None are in lowercase and we must write them as they are.

Finally, the list of all the keywords is:

False await else import pass
None break except in raise
True class finally is return
And continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield

FAQ on Values, Variables and Keywords

Question 1: What is the difference between keyword and variable in Python?

Answer 1: Keywords refer to the reserved words of Python which have a special fixed meaning for the interpreter which we call keywords. Similarly, we cannot use any keyword as an identifier. On the other hand, the variable is like a container that stores values that we can access or change.

Question 2: What are the keywords in Python?

Answer: Python keywords refer to special reserved words which have particular meanings and purposes. Thus, we cannot use them for anything but those specific purposes. Moreover, these keywords are always available. Meaning to say, you will never have to import them into your code. In addition, please remember that Python keywords are different from Python’s built-in functions and types.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

One response to “Values, Variables and Keywords”

  1. Brij Bhushan says:

    Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I also love your website because all type of information is available in your blogs. You made my day. Thanks you for everything. I have bookmarked more article from this website. Such a nice blog you are providing.

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.