Python Variables | A Complete Guide on Python Variables


Python Variables – Table of Content

Python Variable 

A name that is used to refer to the memory location in a programming language is called a variable. Python Variables are also termed storage containers in other words. Variables in python are ‘statically typed’ meaning a user does not need to create variables while coding. The variables get declared themselves whenever a value is assigned to them. There are 4 main types of variables: integer, long integer, string, and float. Hence, we cannot have any type of command which can create a variable.

The main use of variables in python is to store values as a reserved memory container. In this article, we will understand what variables actually are, how identifier naming is done while working with the variables, declaration of a variable in python, identifying objects using variables, different types of variable names and their types such as local variable, global variable, object reference and how to finally delete a variable once created.

Become a Python Certified professional by learning this HKR Python Training!

Identifier Naming

Variables in python are just an example of an identifier that will recognize the literals which are being used in the program. They work according to a set of rules which are mentioned below:

  • The name of an identifier is always case-sensitive. For example, ‘WelcomeToHKR’ and ‘WelcometoHKR’ are not the same.
  • The identifier’s initial character should either be an alphabet or an underscore(_)
  • The alphabet following the initial alphabet
  • The identifiers cannot have special characters in them including white spaces.
  • The name of the identifier need not be the same as the keyword defined in the programming language.
  • Some examples to correct identifiers are: x301, _x, x_0, etc.
  • Some examples of incorrect identifiers are 2y, 1%r, =34, etc.

Declaring a Variable in Python

As we have discussed, there is no need to create a variable unless there needs to be a value assigned to it. One more thing which is very important to note is that variables need not be declared in a specific type. The type of variable can even be changed after the user declares them. We use the equals (=) operator to assign a value to the variable.

Let us take an example of python code below to understand how we can declare variables in python:

a = 10

b = "HKR"

print(a)

print(b)

Output:

10

HKR

The user can also re-declare the variable after creating it. Check out the python code below:

Number = 10

print("Before declaring the variable: ", Number)

Number = 12

 
print("After re-declaring the variable:", Number)

Output:

Before declaring the variable:  10

After re-declaring the variable: 12

Object Identification Using Variables

Every variable created in python is unique. It is not possible to have two same variables for 2 different objects. There is a built-in function in python id() which identifies the id of the variable meaning whether it’s defined already or it’s new.

Let us take an example of python code below and understand how object identification is done in Python using variables:

x = 10 

y = x  

print(id(x))  

print(id(y))  

x = 50  

print(id(x))

Output:

9756512

9756512

9757792

Here in the code above, the user has assigned y = x, where both x and y are pointing to the same object. With the use of id() function, it will also return the same number.

Hence, we will re-assign x to 50; then it is termed as a new object identifier and will have a new changed output.

Variable Names

We have already discussed how variables work with programming languages and how we can declare them along with assigning value to them. The names of variables may be of any length having a lowercase (a to z), an uppercase (A to Z), any digits from 0 to 9, or an underscore (_).

Let us take an example below and see how variable names can work in python.

Name = "Y" 

name = "X"  

naMe = "Z"  

NAME = "M"  

n_a_m_e = "L"  

_name = "N"  

n_a_m_e = "L"

name_ = "O"  

_name_ = "P"  

na56me = "R"  

  

print(Name,name,naMe,NAME,n_a_m_e, NAME, _name, n_a_m_e, name_,_name, na56me)  

Output:

Y X Z M L M N L O N R

As we can see in the example above, the user has declared some valid variable names such as naMe, _name, etc. But this procedure might create confusion when one reads the code so therefore this is not mostly recommended. The user should try making the variable name a little descriptive hence making the code more readable.

The multi-keywords can be created as:

Pascal Case – In this, the first word is capitalized along with the word or abbreviation in the middle of the word. For example: WelcomeToHKR, HowAreYou, etc.

Snake Case – The words are separated using underscore(_) in the snake case. For example Welcome_To_HKR, How_Are_You, etc.

Camel Case – Mostly like the pascal case, each word in the middle will begin with a capital letter. For example: welcomeToHKR, howAreYou, etc.

Acquire Apache NIFI certification by enrolling in the HKR Apache NIFI Training program in Hyderabad!

Python Training Certification

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Python Variable Types

There are two types of variables in python: Local variable and Global variable.

Let us understand more about these and understand them in depth.

1. Local Variables: These types of variables are always defined inside the function. Their scope is also limited to the function only. Check out the example below of how we can make use of local variables in a python code:

def add():  

    x = 10  

    y = 20  

    z = x + y  

    print("The sum of numbers is:", z)  

add()

Output:

The sum of numbers is: 30

As we can clearly see in the code above, the user declared a function as add() to assign it to variables within the function. The variables will be called local variables as they will have scope inside the function only. If the user tries to declare them outside of the function, he will get a code error called NameError: name ‘x’ is not defined

2. Global Variables: These types of variables can be used both inside as well as outside the function. Their scope lies in the complete program. In case it is not mentioned in the code, the global variables are by default declared outside the function. In case the user forgets to mention the variable type, it will be local by default. Check out the example below of how we can make use of global variables in a python code:

a = 10  

  

def mainFunction():  

    global a  

    print(a)  

    a="Welcome To HKR Training"  

    print(a)    

mainFunction()  

print(a) 

Output:

10

Welcome To HKR Training

Welcome To HKR Training

As we can see in the code above, the user has declared a global variable a and a value is assigned to it. Then the user defines a function and it accesses the pre-declared variable inside the function by making use of the global keyword.

Variable type in Python

The data types in a programming language basically states the operations that are to be performed on the given data.  As we know that python works for objects in the programming, variables work as the objects only for the data types.

Here is a list of few data types that work with python variables:

  • Numeric
  • Sequence
  • Boolean
  • Set
  • Dictionary

assigned to different values.

Let us consider an example below and see how we can use various data types with variables:

var1 = 12345

print("Numeric data is : ", var1)

String = 'Welcome to HKR'

print("Topic is Python Variables")

print(String)

print(type(True))

print(type(False))

set = set("HKR Trainings")

print("\nSet with the use of String: ")

print(set)  

Dict1 = {1: 'HKR', 2: 'Welcomes', 3: 'You'}

print("\nDictionary with the use of Integer Keys: ")

print(Dict1)

 

Output:

Numeric data is :  12345

Topic is Python Variables

Welcome to HKR

<class 'bool'>

<class 'bool'>

Set with the use of String: 

{'g', 'a', 'r', 'R', 'i', 'n', 'T', ' ', 's', 'X', 'H'}

Dictionary with the use of Integer Keys: 

{1: 'HKR', 2: 'Welcomes', 3: 'You'}

Top 30 frequently asked Python Interview Questions!

HKR Trainings Logo

Subscribe to our YouTube channel to get new updates..!

Object Reference

a=10

b=a

If we take the example above, we understand that the code creates an object to represent the value 10. Then, it is creating the variable in case it does not exist. It is made as a reference to this new object having a value of 10. In the second line, there is a creation of another variable b however it isn’t assigned with a but is made in reference to that object that an actually does.

Multiple Assignment

In python, a user is allowed to assign a single value to multiple variables. The user can perform multiple assignments in two different ways. It is done either by assigning one value to different variables or can also be done by having different variables assigned to different values.

Let us see an example below of how we can assign single value to multiple variables:

a=b=c=10    

print(a)    

print(b)    

print(c)

Output:

10

10

10

Now let us see another example of how we can assign multiple values to multiple variables:

a, b, c = 10, 20, "WelcomeToHKR"

print(a)

print(b)

print(c)

Output:

10

20

WelcomeToHKR

As the variables appear, the values will be assigned in the same manner only.

Deleting a Variable

A variable can be deleted using the ‘del’ keyword.  

Let us see an example of how we can delete a variable using python:

a = 10  

print(a)  

del a  

print(a)

Output:

Traceback (most recent call last):

  File "./prog.py", line 4, in

NameError: name 'a' is not defined

Python Training Certification

Weekday / Weekend Batches

Conclusion

Through this article, we have understood what a variable is, how we can declare a variable inside a function and how we can assign a value to the variable. The article will help you clear all your doubts about python variables along with the basic rules that variables come up with.

Related Articles

  1. Python Ogre



Source link

Leave a Reply

Subscribe to Our Newsletter

Get our latest articles delivered straight to your inbox. No spam, we promise.

Recent Reviews


What is CCBA

CCBA stands for Certificate of Capability in Business Analysis. It is a certification awarded by IIBA for mid-level business analysis professionals. It is one of the most reputable certifications all over the world. A person can qualify for the CCBA examination if they are able to contribute a minimum work experience of 3750 hours with the BABOK Guide. Another requirement is a minimum of 500 hours in four of the six knowledge areas or 900 hours in two of the six knowledge areas.

Become a CCBA Certified professional by learning this HKR CCBA Training !

What is CBAP

CBAP stands for Certified Business Analysis Professional. It is a certification awarded by IIBA for senior-level business analysis professionals. It is the best-recognized certification for business analysts. A person can apply for the CBAP examination if they have more experience than the requirement of CCBA. In addition to the BABOK guide, BA work experience of a minimum of 7500 hours is needed over the past ten years. The candidate must be capable of documenting 900 hours in four of the six knowledge areas.

CCBA Certification Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Difference between CCBA & CBAP certification

CCBA: 

Certification Body: IIBA
Aimed at: Mid-level Business Analysis Professionals
Curriculum: BABoK 3.0
Work Experience: At least 3,750 hours of BA work experience aligned to the BABOK guide during the past seven years.
Knowledge Area Expertise: At least 900 hours within each of 2 of the six knowledge areas or at least 500 hours within each of the 4 of the six knowledge areas.
Professional development Training Hours: At least 21 hours of Professional development training over the last four years.
Reference required: Two references from a customer, career manager or CBAP recipient.
Exam Mode: Online
Exam fees:
Application Fee: $125
Certification Fee: $325 for members
            $450 for non-members

Exam Duration: 3 hours
Questions Pattern: Scenario-based Multiple choice questions
No. of Questions: 130
Difficulty level: Medium
In order to answer the questions, the candidate must do some analysis.

Brand value: Medium
Focuses on:
Elicitation and Collaboration – 20%
Requirements Analysis and Design Definition – 32%
Strategy Analysis – 12%
Business Analysis Planning and Monitoring – 12%
Requirements Life Cycle Management – 18%
Solution Evaluation – 6%

CBAP:

Certification Body: IIBA
Aimed at: Senior-level Business Analysis Professionals
Curriculum: BABoK 3.0
Work Experience: At least 7500 hours of BA work experience aligned to the BABOK guide during the past ten years.
Knowledge Area Expertise: At least 900 hours within each of 4 of the six knowledge areas.
Professional development Training Hours: At least 35 hours of Professional development training over the last four years.
Reference required: Two references from a customer, career manager or CBAP recipient.
Exam Mode: Online
Exam fees:
Application Fee: $125
Certification Fee: $325 for members
                         $450 for non-members

Exam Duration: 3.5 hours
Questions Pattern: Multiple choice questions related to the case.
No. of Questions: 120
Difficulty level: High
In order to answer the questions, the candidate must perform a good analysis.

Brand value: High
Organizations recognize this certification in their framework of competence.

Focuses on:
Elicitation and Collaboration – 12%
Requirements Analysis and Design Definition – 30%
Strategy Analysis – 15%
Business Analysis Planning and Monitoring – 14%
Requirements Life Cycle Management – 15%
Solution Evaluation – 14%

HKR Trainings Logo

Subscribe to our YouTube channel to get new updates..!

Why do the Organizations hire CCBA or CBAP professionals

CCBA or CBAP professionals offer a number of benefits to the organization. 

  • CCBA and CBAP provide opportunities for staff to advance, grow their careers and gain recognition.
  • The candidate is recognized for their skills and knowledge. They ensure that suppliers, customers, staff, investors, and competitors comply with the industry standard.
  • They provide assurance to stakeholders that the company is operating effectively.
  • They use industry-based standardized Business Analytics techniques to achieve better outcomes which increase efficiency and consistency and are more reliable.
  • The calculation process will help to improve staff responsibility, commitments, and motivation.
  • The implementation and development of business analysis practices are conducted in accordance with the Business Analysis Body of Knowledge (BABOK) guidelines.
  • Customers and business partners receive recognition from professional business analysts.

How to choose between CCBA and CBAP

While CBAP and CCBA Certification are related to business analysis, there is a significant difference between the two.

Both exams have a different focus. The CBAP exam consists of situation-based questions which ask the candidate to apply the knowledge they have acquired from BABOK. At the same time, the CCBA exam contains mostly factual and knowledge questions about techniques and tasks in BABOK. But CBAP study material can be used for preparing for the CCBA exam. If your experience matches with the CBAP, then proceeding with CBAP is the better choice than CCBA. If you cannot acquire the same experience as the CBAP, consider CCBA as a temporary certification that you can still be successful in leveraging your skills and broadening your experience in business analysis and Work towards CABP eligibility prior to the recertification period.

The CBAP is more challenging to attain than the CCBA; consequently, CBAP is more prestigious.

Preparation for the examination – CCBA vs CBAP

CCBA

One can attend the CCBA exam in the comfort of their home. Further, the preparation format for the CCBA exam requires covering the following topics thoroughly.:-

  • BABOK V-3 Overview
  • BA Process & Techniques
  • Collaboration & Elicitation
  • Strategy Analysis
  • Requirement Analysis & Design Definition
  • Estimation of solutions
  • Business Analysis – Monitoring & Planning.
  • Life Cycle Management needs
    So, after covering all these topics thoroughly, you will finally be ready to attempt the exam. While preparing for the CCBA exam, you need to remember some key points.
    1. Must have a good grasp of the BABOK guide version 3. Also, the aspirant should complete the Certification training well.
    2. To check the knowledge gaps, you must regularly attempt the mock tests and check the dashboard report.

CBAP

Like CCBA, you can attend the CBAP exam from anywhere and anytime, as it is an online exam. The exam duration is about 3.5 hours and includes 120 MCQ questions. Further, the preparation format for the CBAP exam also requires you to cover similar topics as we see in CCBA. The topics are similar, but the coverage ratio for the CBAP exam is different compared to the CCBA exam.
A survey says that an applicant must prepare atleast 90 to 120 hours for CCBA vs CBAP exam. Further, it would be better if an applicant gives 3 to 6 months to prepare for this exam. It would be sufficient time for better preparation. Moreover, taking mock tests with regular practice can give more confidence to clear the final exam.

Benefits of Getting The Certification

CCBA

CCBA Certification gives immense benefits as a Business Analyst, showcasing your BA skills and abilities. You can avail lots of help after getting certified in CCBA.

  • It will give more opportunities as CCBA is a globally recognized certification.
  • You can effectively and actively implement various Business Analysis skills.
  • It will present your BA skills and specific industry standards for which global employers search.
  • The intensive learning and preparation will give an extensive knowledge base. The various concepts will expand your knowledge and help businesses make the right decisions.
  • These credentials will give you more confidence to work in a BA profile and help your career graph to grow.
  • You can get a higher salary package or can see a hike in your existing pay with this Certification. Also, there are many chances to get promoted to the next level in a career.
  • Further, you will enjoy the vast skill-set with in-depth knowledge.
  • Also, the entity will get into a leading position with your efforts and skill base. Therefore, the business will bring more projects to work.

CBAP

CBAP credentials also offer many benefits as a Certified Business Analysis Professional, which showcases your BA skills. You can expect a lot of benefits after getting this certificate.

  • This Certification will fill confidence within you by giving a higher weightage in your workplace.
  • A practising BA can take advantage of the CBAP credentials.
  • With the CBAP credentials, you can get more recognition, proficiency, ability, and skills.
  • You will be able to implement effective business strategies.
  • Further, this certificate will commit to the BA profession to work more effectively.
  • Moreover, CBAP is the most prestigious Certification everyone wishes to pursue to become an expert Business Analyst.
  • This Certification will further add more weight to your career growth.
  • Thus, in the CCBA vs CBAP certifications, many aspirants prefer CBAP as the best career-oriented skill. It is the most recognized credential that every BA professional expects to get.

[ Related Article: certified business analysis professional ]

CCBA Certification Training

Weekday / Weekend Batches

Conclusion

As business analysis is an essential element of any organization determining business needs, a professional business analyst is a must. Because of this, there is a high demand for highly qualified and expert analysts. Appearing for examinations like CCBA, CBAP makes it easy to prove your skills and enhance your profile to be selected from a reputable company. Based on the knowledge acquired during the certification course, the candidate could shape their future and lead the organization to heights. So choose a certification that’s right for you.



Source link