Instagram
youtube
Facebook
Twitter

Python Variables & Keywords

What are python Variables?

A python variable is a way to store values to a defined memory location. Variables in Python scripts are used to provide values to the computer. Variables in Python are dynamically typed which means you don't have to define the data type of variable in python.

eg,

To define an integer variable you just have to write the name of the variable with equals to symbol and value.

a = 50

print(a)

To define a variable with a string value you just have to write the variable name the string in quotes.

name = "Codersdaily" 

print(name)

 

Rules to define a variable:

  • A variable name can't start with a symbol.

  • Always start defining variable names with a small letter.

  • If the variable name contains multiple words separate them with a small case letter for the first word and a second word starting with an upper case letter.

  • Always try to make variable name relatable with the type of value it is holding. For eg, if you are defining a variable age it should hold the value of age.

  • Python variables cant be a keyword. For eg, class, print, def, etc.

 

Examples to define variables

#Example1
a = 25
b = 25
print(a)
print(b)

 

#Example2
name = "Codersdaily"
print(name)

 

What are Python Keywords?

Python is a high-level programming language that provides a wide range of features and capabilities to developers. Like other programming languages, Python has a set of reserved words, known as keywords, that have a special meaning and cannot be used for naming variables, functions, or classes. In this tutorial, we will explore the Python keywords in detail.

Python has a total of 35 keywords as of Python 3.10. Here's a list of all the Python keywords:

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

These keywords are used for specific purposes in Python and cannot be redefined or used as variable names. Here's a brief overview of each keyword:

  • False, None, and True: These keywords are used to represent the Boolean values False, None, and True, respectively.

  • and, or, not: These keywords are used for Boolean logic and provide the functionality of logical AND, OR, and NOT operations.

  • as: This keyword is used for aliasing a module, function, or class.

  • assert: This keyword is used for debugging purposes to check if a certain condition is true, and if not, it raises an exception.

  • async and await: These keywords are used for asynchronous programming and allow us to write asynchronous code in a synchronous manner.

  • break and continue: These keywords are used in loop statements to break out of a loop or skip to the next iteration, respectively.

  • class: This keyword is used to define a class in Python.

  • def: This keyword is used to define a function in Python.

  • del: This keyword is used to delete a variable, object, or attribute.

  • elif: This keyword is used in conditional statements as a part of the if-elif-else block.

  • else: This keyword is used in conditional statements as a part of the if-elif-else block.

  • except: This keyword is used in exception handling to catch an exception.

  • finally: This keyword is used in exception handling to execute code after a try-except block, regardless of whether an exception was raised or not.

  • for: This keyword is used to create a loop that iterates over a sequence.

  • from and import: These keywords are used to import modules or specific functions or classes from a module.

  • global: This keyword is used to define a variable as global.

  • if: This keyword is used to define a conditional statement.

  • in: This keyword is used to check if a value exists in a sequence.

  • is: This keyword is used to check if two objects are the same object.

  • lambda: This keyword is used to define anonymous functions.

  • nonlocal: This keyword is used to define a variable as nonlocal.

  • pass: This keyword is used as a placeholder for code that will be added later.

  • raise: This keyword is used to raise an exception.

  • return: This keyword is used to return a value from a function.

  • try: This keyword is used in exception handling to try a block of code.

  • while: This keyword is used to create a loop that runs while a condition is true.

  • with: This keyword is used to define a block of code with a context manager.

  • yield: This keyword is used in generator functions to yield a value.

Now that you are familiar with all the Python keywords, you can use them

 


FAQs 

Q: What is a variable in Python?

A: A variable is a name that refers to a value in memory. In Python, you can assign any value to a variable, such as a number, a string, or even a function.

Q: How do you declare a variable in Python?

A: You declare a variable in Python by using the "=" operator to assign a value to a name. For example: x = 5 assigns the value of 5 to the variable name "x".

Q: Are Python variables case-sensitive?

A: Yes, Python variables are case-sensitive. For example, myVariable and myvariable are two different variables in Python.

Q: What are keywords in Python?

A: Keywords are reserved words in Python that have a specific meaning and cannot be used as variable names. Some examples of keywords in Python include "if", "else", "while", "for", "and", "or", and "not".

Q: Can you use a keyword as a variable name in Python?

A: No, you cannot use a keyword as a variable name in Python. If you try to do so, you will get a syntax error.

Q: How many keywords are there in Python?

A: There are 35 keywords in Python 3.10 as of September 2021.

Q: What is the purpose of keywords in Python?

A: Keywords are used to define the syntax and structure of the Python language. They have specific meanings and are used to control the flow of the program, define functions and classes, and perform other essential tasks in Python.