DEV Community

Ajay
Ajay

Posted on

A learning series of Python , Day-2

What is Identifier ?

A name in python program is known as Identifier / Reference variable /variable
A used-defined name to discribe something to machine.

Rules of declaring Identifiers

  • Starts with alphabets[lowerCase / upperCase ]
  • Digits are permitted , but should not starts with digits
  • Using UnderScore(_) -Should not to use reserved Keywords
###Valid Identifiers
Name = "Ajay"
name = "Ajay"
name1 = Ajay"
newName = "Ajay"
New_name = "Ajay"
_Name = "Ajay"
__Name = "ajay"
__name__ = "ajay"

Enter fullscreen mode Exit fullscreen mode
###Invalid Indentifers
1name = "ajay"
new name = "ajay"
class ="ajay" # class is the reserved keyword
new-Name = "ajay"

Enter fullscreen mode Exit fullscreen mode

Rule of UnderScore in Python

  • _name --->(#private):

A single underscore indicates that a variable or method is intended for internal use.
Please treat this as internal

class Student:
    def __init__(self):
        self._marks = 90

s = Student()
print(s._marks)
Enter fullscreen mode Exit fullscreen mode
  • __name --->(#Strictly private):

A double underscore before a name triggers name mangling. Python changes the attribute's internal name to reduce accidental access and naming conflicts.

Reduce accidental access and naming conflicts

class Student:
    def __init__(self):
        self.__marks = 90

s = Student()

# print(s.__marks)  # AttributeError
print(s._Student__marks)

Enter fullscreen mode Exit fullscreen mode
  • name --->(Special methods):

Double underscores on both sides identify special names used by Python. They are often called dunder names.
Python special method or attribute

class Student:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

s = Student("Ajay")
print(s)
Enter fullscreen mode Exit fullscreen mode

Reserved Words

Reserved words are known as keywords are predefined, case Senstive identifier used in language to fidn the syntax and structure.

  • Reserved words should not be an identifies , function , object
  • 30+ keywords in python

Reserved words are...

['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']

DataType in python

  • Data types classify the type of value is stored in the variable.
  • Data --> Something giving to the computer -Type ---> Categorising(number , words)
  • Python is dynamically typed, meaning you do not need to explicitly declare a variable's type; the interpreter automatically infers it based on the assigned value.

1)Numeric type

  • Int --(..-2,-1,0,1,2..)
  • Float--(1.22,2.44,..)
  • complex --(..,2+3j , 3+7y,..)

2)Text type

  • string message = "Hello, World!"

3)Sequence Type

  • list -["apple", "banana", "cherry"] -mutable
  • tuple - (20.3 , 30.0) - immutable
  • range - (0,10) -start to end

4)Mapping Type (dictionary)

  • It used to store in key-value pairs inside curly brackets. user_profile = { "username": "coder123", "followers": 1500 }

5)Set Types

Sets are unordered collections where every item must be completely unique. They automatically strip out duplicates.

unique_ids = {101, 102, 103, 101}

6)Boolean

Evaluates to either True or False

Top comments (0)