Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

Identifiers in Python: Rules, Examples, and Naming Conventions

When you start writing Python programs, you will create variables to store information, functions to perform tasks, and classes to organize your code.

But how do you give names to these elements?

For example:

student_name = "Rahul"
student_age = 20
total_marks = 450

Here, student_name, student_age, and total_marks are names used to identify different variables. In Python, these names are called identifiers.

Understanding identifiers is important because choosing valid and meaningful names helps you write programs that are easier to read, understand, and maintain.

In this article, we will learn what identifiers are, the rules for naming them, valid and invalid examples, and the naming conventions followed by Python programmers.

1. What Is an Identifier in Python?

An identifier is a name used to identify a variable, function, class, or another named object in a Python program.

Consider this example:

name = "Amit"
age = 19

def display():
    print(name, age)

In this program:

  • name is an identifier for a variable.

  • age is an identifier for a variable.

  • display is an identifier for a function.

Identifiers help us refer to different elements of a program by name.

2. Why Are Identifiers Important?

Imagine writing a program that stores student information, calculates marks, and displays results.

You could use short names such as a, b, and c, but these names would not clearly explain what the variables represent.

Compare these examples.

Example 1: Unclear identifiers

a = "Rahul"
b = 85
c = 90

Example 2: Meaningful identifiers

student_name = "Rahul"
english_marks = 85
maths_marks = 90

The second example is easier to understand because each identifier describes its purpose.

Meaningful identifiers become especially useful when you work on large projects involving databases, websites, data science, and artificial intelligence.

3. Rules for Naming Identifiers in Python

Python follows specific rules when creating identifiers. Let's understand them one by one.

Rule 1: An identifier can contain letters, digits, and underscores

Valid examples:

student
student1
student_name
marks2026

These names are valid because they use permitted characters.

Rule 2: An identifier cannot start with a digit

Invalid examples:

1student = "Rahul"
2026marks = 90

Python does not allow identifiers to begin with a number.

Correct examples:

student1 = "Rahul"
marks2026 = 90

The digits can appear after the first character.

Rule 3: Spaces are not allowed

Invalid example:

student name = "Rahul"

Python treats the space as a separator, so this is not a valid variable assignment.

Correct example:

student_name = "Rahul"

Use an underscore when you want to separate words in a variable name.

Rule 4: Special characters are generally not allowed

Characters such as @, #, $, and % cannot be used within ordinary Python identifiers.

Invalid examples:

student@name = "Rahul"
total#marks = 450
price$ = 100

Correct examples:

student_name = "Rahul"
total_marks = 450
price = 100

The underscore _ is allowed and is commonly used to separate words.

Rule 5: Python keywords cannot be used as identifiers

Keywords have special meanings in Python, so they cannot be used as ordinary variable names.

Invalid examples:

if = 10
for = 20
class = "Python"

Correct examples:

number = 10
count = 20
course_name = "Python"

Choose a different name whenever a word is reserved by Python.

Rule 6: Identifiers are case-sensitive

Python treats uppercase and lowercase letters as different.

For example:

name = "Rahul"
Name = "Amit"
NAME = "Priya"

print(name)
print(Name)
print(NAME)

Output:

Rahul
Amit
Priya

The identifiers name, Name, and NAME refer to three different names.

For this reason, use capitalization consistently throughout your programs.

4. Valid and Invalid Identifiers

The following table summarizes common naming examples.

IdentifierValid or Invalid?Reason
studentValidContains letters
student1ValidDigit appears after the first character
student_nameValidUnderscore is allowed
_countValidAn identifier can start with an underscore
totalMarksValidUses letters with no invalid characters
1studentInvalidStarts with a digit
student nameInvalidContains a space
student@nameInvalidContains @
ifInvalidReserved keyword
total-marksInvalidContains a hyphen

Important: The rules above apply to ordinary Python identifiers. Python also supports Unicode letters, but beginners can generally use English letters, digits, and underscores for clarity.

5. Naming Conventions in Python

A naming rule determines whether an identifier is valid. A naming convention is a commonly followed style that makes code easier to read.

Python programmers often follow these conventions.

A. Use descriptive names for variables

Instead of:

x = 500

Prefer:

total_marks = 500

The second name clearly explains what the value represents.

B. Use lowercase letters for variable names

A common Python style is to write variable names in lowercase.

Example:

student_name = "Rahul"
student_age = 20

This is easier to read and follows the usual Python naming convention.

C. Use underscores to separate words

Python commonly uses snake_case for variables and functions.

Example:

first_name = "Rahul"
total_price = 1500
calculate_average = 90

Each word is separated by an underscore.

D. Use capitalized words for class names

Python commonly uses PascalCase for class names, where each word starts with a capital letter.

Example:

class StudentDetails:
    pass

Here, StudentDetails is the class name.

You will learn more about classes when studying object-oriented programming.

E. Use uppercase names for constants

Programmers commonly use uppercase letters for names intended to represent constants.

Example:

MAX_MARKS = 100
PI = 3.14159

Python does not automatically prevent these names from being reassigned. Uppercase letters communicate the programmer's intention that the values should remain unchanged.

6. Identifiers in a Python Program

Let's look at a complete example.

student_name = "Rahul"
english_marks = 85
computer_marks = 95

total_marks = english_marks + computer_marks

print("Student Name:", student_name)
print("Total Marks:", total_marks)

Output:

Student Name: Rahul
Total Marks: 180

Let's identify the names used in the program:

  • student_name identifies the student's name variable.

  • english_marks identifies the English marks variable.

  • computer_marks identifies the computer marks variable.

  • total_marks identifies the variable that stores the sum.

Each identifier helps explain the purpose of the corresponding variable.

7. Can an Identifier Have Only an Underscore?

Yes. A single underscore is a valid identifier in Python.

For example:

_ = 100

print(_)

Output:

100

However, a single underscore is also used by Python programmers for certain conventions, such as indicating that a value is intentionally being ignored.

Example:

name, _ = ("Rahul", 20)

print(name)

Output:

Rahul

In this example, the second value is assigned to _ because the program does not need to use it.

8. Common Mistakes Beginners Should Avoid

Mistake 1: Starting a name with a number

Incorrect:

2marks = 80

Correct:

marks2 = 80

Mistake 2: Using spaces in names

Incorrect:

student age = 20

Correct:

student_age = 20

Mistake 3: Using a keyword as a variable name

Incorrect:

while = 5

Correct:

count = 5

Mistake 4: Using confusing names

Less descriptive:

a = 500

More descriptive:

total_marks = 500

Mistake 5: Mixing capitalization inconsistently

Consider:

student_name = "Rahul"

print(Student_Name)

This will raise a NameError because Student_Name and student_name are different identifiers.

Correct:

student_name = "Rahul"

print(student_name)

9. Practice Questions

Test your understanding with these questions.

Question 1: What is an identifier in Python?

Question 2: Write any five valid identifiers.

Question 3: Identify the invalid identifiers:

  • student_name

  • 2student

  • total_marks

  • student age

  • class

Question 4: Why can an identifier not start with a digit?

Question 5: Are age, Age, and AGE the same identifier?

Question 6: Rewrite the following identifiers correctly:

  • student name

  • 1number

  • total-marks

  • if

Question 7: What naming style is commonly used for Python variables?

Question 8: Write a Python program that creates identifiers for a student's name, roll number, and marks, then displays their values.

10. Practice Program: Student Information

Try this simple program:

student_name = input("Enter student name: ")
roll_number = int(input("Enter roll number: "))
marks = float(input("Enter marks: "))

print("Student Name:", student_name)
print("Roll Number:", roll_number)
print("Marks:", marks)

Example output:

Enter student name: Rahul
Enter roll number: 12
Enter marks: 87.5
Student Name: Rahul
Roll Number: 12
Marks: 87.5

Notice how descriptive identifiers make the program easy to understand.

Final Thoughts

Identifiers are the names we use to identify variables, functions, classes, and other named objects in Python.

Remember these essential rules:

  • Start an identifier with a letter or underscore, not a digit.

  • Do not use spaces or ordinary special characters in identifiers.

  • Never use a reserved keyword as an ordinary variable name.

  • Remember that Python is case-sensitive.

  • Choose descriptive names and follow consistent naming conventions.

The best way to learn identifiers is to practise writing small Python programs and use meaningful names in every example.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? 📌 Readability – Comments make code easier to understand for you and others. 📌 Maintenance – When you revisit your code after months, comments remind you what you were doing. 📌 Collaboration – In team projects, comments explain code to fellow developers. 📌 Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . 💻 Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } 🧠 Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

💡 Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. 🧑‍💻 Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...