csaccept.com is a computer awareness website dedicated to providing reliable and easy-to-understand information about computer technology and digital safety. The website focuses on educating students, beginners, and general users about computer basics, cyber security, emerging technologies, and practical IT skills. Through informative articles, quizzes, and real-life examples, csaccept.com aims to increase digital literacy and help users stay safe and confident in today’s technology-driven world.

Full details of Variables , Identifier , Data Types , Keywords & Operators in Python

Variables in Python:

  • In Python, variables are used to store values that can be used later in a program.
  • You can think of variables as containers that hold data.
  • What makes Python unique is that you don’t need to explicitly declare the type of a variable.
  • You simply assign a value to a variable using the “=” operator.

For example, you can create a variable called “name” and assign it a value like this:

name = “Yadnyesh”

Here, “name” is the variable name, and “Yadnyesh” is the value assigned to it. Python will automatically determine the type of variable based on the value assigned to it.

Variables in Python can hold different types of data, such as numbers, strings, lists or even more complex objects. You can change the value of a variable at any time by assigning a new value to it.

For instance:

age = 25

age = 26   # Updating the value of age variable.

Python also allows you to perform operations on variables.
For example, you can add, subtract, multiply, or divide variables containing numbers. You can even combine variables of different types using operators. For instance:

x = 5

y = 3

z = x + y   # The value of ‘z’ will be 8.

greeting = “Hello”

name = “John”

message = greeting + ” ” + name   # The value of ‘message’

will be “Hello John”.

Variables provide a way to store and manipulate data in Python, making it easier to work with information throughout your program. By giving meaningful names to variables, you can make your code more readable and understandable.


Identifier in Python:

In Python, an identifier is a name used to identify a variable, function, class, module, or any other user-defined object. An identifier can be made up of letters (both uppercase and lowercase), digits and underscores (_). However, it must start with a letter or an underscore.

Here are some important rules to keep in mind when working with identifiers in Python:

  1. Valid characters:
    An identifier can contain letters (a–z, A–Z), digits (0–9) and underscores (_). It cannot contain spaces, special characters like @, #, or $.
  2. Case Sensitivity:
    Python is case-sensitive, meaning uppercase and lowercase letters are considered different. So, “myVar” and “myvar” are treated as two different identifiers.
  3. Reserved Words:
    Python has reserved words, also known as keywords, that have predefined meanings in the language. These words cannot be used as identifiers. Examples of reserved words include “if”, “while” and “def”.
  4. Length:
    Identifiers can be of any length. However, it is recommended to use meaningful and descriptive names that are not excessively long.
  5. Readability:
    It is good practice to choose descriptive names for identifiers that convey their purpose or meaning. This helps make the code more readable and understandable.

Here are some examples of valid identifiers in Python

  • my_variable
  • count
  • total_sum
  • PI
  • Myclass

And here are some examples of invalid identifiers:

  • 123abc (starts with a digit)
  • my-variable (contains a hyphen)
  • if (a reserved word)
  • my var (contains a space)

Understanding and following these rules for identifiers in Python is important to ensure clarity, readability and proper functioning of your code.

Data Types in Python:

Data types in Python refer to the different kinds of values that can be assigned to variables. They determine the nature of the data and the operations that can be performed on them.

Python provides several built-in data types, including: numeric, dictionary, Boolean, set, sequence types and numeric has Integer, Complex Number, Float and sequence type has String, List and Tuple.

Python Data Types

Python Data Types include:

  • Numeric
    • Integer
    • Complex Number
    • Float
  • Dictionary
  • Boolean
  • Set
  • Sequence Type
    • String
    • List
    • Tuple
  1. Numeric

Python supports different numerical data types, including integers (whole numbers), floating-point numbers (decimal numbers), and complex numbers (numbers with real and imaginary parts).

  1. Integers (int):
    Integers represent whole numbers without any fractional part.
    For example:

age = 25

  1. Floating-point Numbers (float):
    Floating-point numbers represent numbers with a decimal point or fractions.
    For example:

pi = 3.14

  1. Complex Numbers (complex):
    Complex numbers have a real and imaginary part.
    For example:

z = 2 + 3j

where j is the suffix for the imaginary part.

  1. Dictionary

Dictionaries are key-value pairs enclosed by curly braces. Each value is associated with a unique key, allowing for efficient lookup and retrieval.
For example:

person = {‘name’: ‘John’, ‘age’: 25, ‘city’: ‘New York’}

  1. Boolean

Boolean (bool): Booleans represent truth values, either True or False. They are used for logical operations and conditions.
For example:

is_valid = True

  1. Set

Sets (set): Sets are unordered collections of unique elements enclosed in curly braces. They are useful for mathematical operations such as union, intersection, and difference.
For example:

fruits = {‘apple’, ‘banana’, ‘orange’}

  1. Sequence Type

Sequences represent a collection of elements and include data types like strings, lists, and tuples. Strings are used to store textual data, while lists and tuples are used to store ordered collections of items.

  1. Strings (str):
    Strings represent sequences of characters enclosed within single or double quotes.
    For example:

name = ‘John’

  1. List (list):
    Lists are ordered sequences of elements enclosed in square brackets. Each element be of any data type. For example:

numbers = [1, 2, 3, 4]

  1. Tuples (tuple):
    Tuples are similar to lists but are immutable, meaning their elements cannot be changed once defined. They are enclosed in parentheses ().

Keywords in Python:

Keywords in Python are special words that have specific meanings and purposes within the Python language. They are reserved and cannot be used as variable names or identifiers.

Keywords play a crucial role in defining the structure and behaviour of Python programs. Keywords are like building blocks that allow us to create conditional statements, loops, functions, classes, handle errors and perform other important operations.

List of all the keywords in Python:

False  await  else  import  pass
None  break  except  in  raise
True  class  finally  is  return
and  continue  for  lambda  try
as  def  from  nonlocal  while
assert  del  global  not  with
async  elif  if  or  yield

Operators in Python:

Operators in Python are symbols or special Characters that are used to perform specific operations on variables and values. Python provides various types of operators to manipulate and work with different data types. Here are some important categories of operators in Python:

Arithmetic Operators

Arithmetic operators in Python are used to perform mathematical calculations on numeric values. The basic arithmetic operators include:

  • Addition (+):
    Adds two operands together.
    For example, if we have a = 10 and b = 10, then a + b equals 20.
  • Subtraction (-):
    Subtracts the second operand from the first operand. If the first operand is smaller than the second operand, the result will be negative.
    For example, if we have a = 20 and b = 5, then a – b equals 15.
  • Division (/):
    Divides the first operand by the other operand and returns the quotient.
    For example, if we have a = 20 and b = 10, then a / b equals 2.0.
  • Multiplication (*):
    Multiplies one operand by the other.
    For example, if we have a = 20 and b = 4, then a * b = 80.
  • Modulus (%)

Returns the remainder after dividing the first operand by the second operand.
For example, if we have a = 20 and b = 10, then a % b = 0.

  • Exponentiation (**) or power

Raises the first operand to the power of the second operand.
For example, if we have a = 2 and b = 3, then a ** b = 8.

  • Floor Division (//)

Provides the floor value of the quotient obtained by dividing the two operands.
It returns the largest integer that is less than or equal to the result.
For example, if we have a = 20 and b = 3, then a // b = 6.

Comparison Operators

Comparison operators in Python are used to compare two values and return a Boolean value (True or False).

  • Equal to (==)

Checks if two operands are equal.

  • Not equal to (!=)

Checks if two operands are not equal.

  • Greater than (>)

Checks if the left operand is greater than the right operand.

  • Less than (<)

Checks if the left operand is less than the right operand.

  • Greater than or equal to (>=)

Checks if the left operand is greater than or equal to the right operand.

  • Less than or equal to (<=)

Checks if the left operand is less than or equal to the right operand.

Assignment Operators

Assignment operators are used to assign values to variables. They include:

  • Equal to (=)
    Assigns the value on the right to the variable on the left.
  • *Compound assignment operators (+=, -=, =, /=)
    Perform the specified arithmetic operation and assign the result to the variable.

Logical Operators

Logical operators in Python are used to perform logical operations on Boolean values.

  • Logical AND (and)
    Returns True if both operands are true, otherwise False.
  • Logical OR (or)
    Returns True if at least one of the operands is true, otherwise False.
  • Logical NOT (not)
    Returns the opposite Boolean value of the operand.

Bitwise Operators

Bitwise operators perform operations on individual bits of binary numbers.

  • Bitwise AND (&) Performs a bitwise AND operation on the binary representations of the operands.
  • Bitwise OR (|): Performs a bitwise OR operation on the binary representations of the operands.
  • Bitwise XOR (^): Performs a bitwise exclusive OR operation on the binary representation of the operands.
  • Bitwise Complement (~): Inverts the bits of the operand.
  • Left Shift (<<): Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
  • Right Shift (>>): Shifts the bits of the left operand to the right by the number of positions specified by the right operand.

Membership Operators

Membership operators are used to test whether a value is a member of a sequence.

  • in: Returns True if a value is present in the sequence.
  • not in: Returns True if a value is not present in the sequence.

Identity Operators

Identity operators are used to compare the identity of two objects.

  • is: Returns True if both operands refer to the same object.
  • is not: Returns True if both operands do not refer to the same object.

 

What is Python? || Features of Python, Applications of Python, Python Installation, First Python Program