Variables in python

Variables in Python

Introduction

“Stop wishing, start doing”

Now, you must be commendable in the basic data types of python.

Moving further, we will be briefing about variables in detail.

Variables

Variables are containers for storing data.

In python we do not have a command to declare variables, they are created at the time a value is assigned to them. 

We no need to explicitly specify a variable data type. It is automatically assigned when creating them according to the value and we can also change the value and its data type whenever needed.

Rules for naming a variable

  • Variable name should start with a letter or underscore(_) character.
  • A variable name cannot start with a number but can contain numbers in between.
  • No special characters are allowed inside a variable name except underscore(_).
  • Space is not allowed in between the variable name.
  • Variable names are case-sensitive.
  • It is always advisable to create a meaningful variable name.

Multi-word variable name

While writing a variable name, there can be a need to write a variable name with many words, but since space is not allowed in between variable names 

It can become tough to read and understand the meaning of the variable name, so we can follow any one of the following patterns while selecting a variable name.

Camel case

Each word except the first starts with a capital letter.

See also  21 Strategy Game

Example:

myLuckyNumber=10

Pascal case

Each word starts with a capital letter.

Example:

MyLuckyNumber=10

Snake case

Each word is separated by an underscore character.

Example:

my_lucky_number=10

Best practice

I always suggest you can start using any type of case, which is appealing to you. However, variables are not the only ones where we use names, so the python style guide also known as PEP8 gives us the following suggestions,

  • Snake Case should be in use for functions and variable names.
  • Pascal Case should be in use for class names.

So, moving further we can follow the same as requested by PEP8 guidelines.

Assign values

Type 1

Equal to(=) is called an assignment operator in python.

Example

my_favorite_number=10

Here the value 10 is assigned to the variable my_favorite_number.

So, moving further we can use my_favorite_number anywhere you need.

We can also change its value whenever needed and the changed value is used for future use.

Type 2

Python also allows us to assign multiple values to multiple variables at the same time.

Example

friut1,friut2,fruit3='apple','orange','grape'

Which will assign fruit1 to apple, fruit2 to orange, and fruit3 to grape respectively.

Make sure, the number of variables should be equal to the number of values provided else you will get an error.

Type 3

You can also assign the same value to multiple variables on a single line.

Example

x=y=z=10

Here, all the three variables x,y, and z are assigned to the same value 10.

Printing variables

If you need to print a variable’s value,

Syntax:

print(variable_name)

If you need to print more than one variable in a single print statement,

See also  50+ Best python projects with source code [2023]

Syntax:

print(variable1 + variable2)

Here, both the variables are concatenated and the output is shown, the only restriction is + operator works best only if you need to concatenate two strings, If you do the same with two integers it will throw an error.

So, the best solution to join many variables of different data types is by using a comma(,) in between them.

Syntax:

print(variable1,variable2)

Example: Let us create two variables for name, age, and hobby and try to print them as a sentence.

name="Pythonista"
age=20
hobby="coding"
print("My name is",name,"of age",age,".\nMy favorite hobby is ",hobby,".")

Output:

My name is Pythonista of age 20.
My favorite hobby is coding.

Practice yourself

  • Create a variable to store your favorite color and print the same.
  • Assign your name and age to two different variables and print it in the following pattern.

Sample Output

My name is <name>
My age is <age>

  • Find the correct variable names from the below list,
My_Favorite_color=”red”
1stNumber=10
Number2=20
My lucky number=30
my-pet-name=”xx”
  • Find the output for the following code,
x=30
print(x)
x=40
X=50
print(x)
x=70
print(X)
  • Write a program to swap two numbers.

Conclusion

Hoping you have got a clear idea regarding variables in python. Don’t forget to work on the Practice yourself. If any doubts you can feel free to ask in the comments section.

2 thoughts on “Variables in Python”

  1. Pingback: Guess the Number Game -

  2. Pingback: Rock Paper Scissor Game -

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock