Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 95 - 2629_Devagiri_C-6
P. 95

    DECLARING AND INITIALISING A VARIABLE
     KEYWORDS AND IDENTIFIERS
                     In Python, you can declare and initialise a variable in one step. For example:

     In Python, keywords and identifiers are two important concepts that define the structure and   x = 5
     naming conventions of a program.  y = 10
                     print("x =", x)
     KEYWORDS
                     print("y =", y)
     Keywords are  reserved words with special   In  this  example,  the  variables  x and y are initialised  with the  values 5 and 10, respectively.
     meanings in Python, used for tasks like control   Python has 35 reserved keywords that cannot   The output will be:
     flow (if, for, while), functions (def) and classes   be used as variable names.
     (class). They cannot  be used  as variable   x = 5
                     y = 10
     names. Here is a list of Python keywords:
                     You can also assign the same value to multiple variables in a single line:
     False  None  True  and  as  assert
     async  await  break  class  continue  def  a = b = 30
     del  elif  else  except  finally  for  print("a =", a)
     from  global  if  import  in  is  print("b =", b)
     lambda  nonlocal  not  or  pass  raise
                     In this case, both a and b will store the value 30 and the output will be:
     return  try  while  with  yield
     You cannot use keywords as variable names. For example, you cannot name a variable ‘if’ or ‘for’.  a = 30
                     b = 30
     IDENTIFIERS     Additionally, you can assign multiple values to multiple variables in a single line. For example:

     An identifier is a name used to uniquely identify a variable, function, class or object in Python. It
                     name, age, city = "John", 25, "London"
     represents the values you store, like numbers or text.
                     print("Name:", name)
     Examples of Identifiers:  print("Age:", age)
                     print("City:", city)
       Variable identifiers: age, score, temperature
                     The output will be:
       Function identifiers: print_message(), calculate_sum()
                     Name: John
       Class identifiers: Person, Student  Age: 25

                     City: London
     VARIABLES IN PYTHON

     In  Python,  variables store values that  can be  accessed  or changed  later. Their names  are   DATA TYPES
     identifiers and you don’t need to specify their type because Python is dynamically typed.
                     A data type defines the kind of value a variable can hold and the operations allowed on it.
     RULES FOR NAMING VARIABLES  For example, names are strings (text) and can be changed to uppercase or lowercase, while age,

     When creating variable names, make sure to follow these rules:  price and marks are numbers, either integers or floats and can be used in calculations.
       Variable names can contain letters (a-z, A-Z), digits (0-9) and underscores (_).  Let’s look at some commonly used data types in Python:

       Variable names must begin with a letter or an underscore (_), but not a number.    int: It represents positive or negative whole numbers (without fractions).
       Variable names are case-sensitive, so ‘age’ and ‘Age’ are considered two different variables.     For example:
                        a = 9
       Variable names cannot use Python keywords (such as if, else, for) as variable names.
       Variable names cannot contain spaces.

                                                                                                                      93
                                                                                                  Python–Start to Code

| 90 91 92 93 94 95 96 97 98 99 100 |