Pages

Sunday, July 28, 2024

Python : About : Variables

 Variables in Python

Variables are used to:

  • Name an object.
  • Remind us about the usability of an object.
  • Allow us to reuse same object in multiple places in our code.

When we say: 

amount = 4543

we are assigning the label amount to the object 4543
OR
We are saying that amount is a reference to the object 4543







Why do we call amount in the above example as a variable?

Ans: Because the object to which amount is pointing to can change, or in case of mutable objects the state of object can change.
















Note: in the above example, memory address of variable amount is same and the state of list it is referring to has changed.

How does variable assignment happen in Python?

Example is shown as below:









So, in Python RHS is evaluated first and then the resultant is assigned to the LHS

















In the example shown above the RHS is evaluated first and then is assigned to LHS.


Variable Naming:

  • Are case sensitive.
  • Must follow certain rules:
    • Start with letter (a-z A-Z) or a underscore (_).
    • Not Start with numbers.
    • Cannot be reserved words like True, False, if, def, and, or ….  
  • Should follow certain conventions as defined in PEP8 Style Guide:


Terminology followed to name a variable:

  • Camel case --> Separate words are distinguished by upper case letters.
    • Example:  minBalance
  • Snake case --> separate words are distinguished by underscores.
    • Example:  min_balance


Mostly Snake case is used in Python programming paradigm.




Python : DataTypes : Core Data Types

Python Core Data Types

It refers to Python's built in Object Types, it comes with python installation.

These are listed as below:

  1. Numbers --> (integer, float, complex numbers, rational fractions with Numerator and Denominator)
  2. Strings
  3. List
  4. Dictionaries
  5. Tuples
  6. Files
  7. Sets
  8. Other Core Types (Eg. Boolean, Types, None)
  9. Program Unit Types (Eg. Funcitons, Classses, modules)
  10. Implementation Related Types (Eg. Compiled Code, Stack Tracebacks)

The above listed Object types would be covered subsequently.

Reasons why we should use Python Built-In types : 
  • Built-in objects make programs easy to write. --- you can use them immediately. You can get a lot of work done with Python’s builtin object types alone.
  • Built-in objects are components of extensions.
  • Built-in objects are often more efficient than custom data structures.



Saturday, July 27, 2024

About Python

 About Python


Features of Python:

  • There are no type declarations in Python, the syntax of the expressions you run determines the type of object you create and use.
  • Once an object is created, its operation set is bind for all time—you can perform only string operations on a string and list operations on a list.
  • Python is both dynamically typed and strongly typed.
    • Dynamically Typed attributes : Python keeps track of types for you automatically instead of requiring declaration code.
      • Eg. you don't have to write String value = "Clarity"  as you do in other language, You only need to write value = "Clarity" and Python would automatically keep track of the type for you i.e string type.
    • Strongly Typed attributes : you can perform on an object only operations that are valid for its type.
      • Eg. You can not do append( ) operation on an integer, it can be done on list or any other type of which its part of.



Thursday, July 25, 2024

Python : Utility : Float : Comparison in Floats

Comparing Floats in Python



Do not compare floats in Python using equality Operator.

As shown in the example below


Here in the example above, we are using format function and its purpose is to display the floating-point numbers up to the digits we have passed in as an argument to it.

It is evident that 0.1 + 0.1 + 0.1 when represented up to 30 digits does not equates to 0.3 and hence it’s not equal to 0.3.


Reason:
0.5 and 0.25 have an exact representation in the floating-point number where as 0.1 do not have an exact representation in floating point number as evident in the above example.



Alternate way to compare floats

Depending upon the use case you are working on, floats can be compared by taking the absolute value of it , subtracting from its actual value and then comparing with some value (tolerance) as per your business use case.



Here in the above example 0.001 is your tolerance value.





Python : About : Variables

  Variables in Python Variables are used to: Name an object. Remind us about the usability of an object. Allow us to reuse same object in mu...