Python Classroom Series – 20/May/2021

Mutability in Python

  • Mutability indicates whether the value can change after creation
  • Basic memory allocation process Preview
  • If we are able to change the value after creation (without impacting memory ) then it is considered to mutable otherwise immutable Preview
Name Type Mutable? Examples
Boolean bool no True, False
Integer int no 47, 45000
Floating Point float no 3.14, 2500.3
Text str no ‘python’, ‘ML’
  • Note: In python we have a Garbage collector which will clean all the unused memory objects Preview

Copying

  • assigning an existing variable a to new variable b just makes new variable b point to the same object that a does.
  • If you either pick a or b and change any of the value & if the type is immutable Preview
  • Lets look at mutable type list Preview

Good Variable Names

  • When ever we are creating variables try to give meaningful names
1. Creating a variable to store the favorite color
i = 'black' # not meaningful
fc = 'black' # not meaningful 
favorite_color = 'black' # meaningful
  • Snake case
my_car
next_course
my_favorite_holiday_spot
  • Pascal case
MyCar
NextCourse
MyFavoriteHolidaySpot
  • Camel Case
myCar
nextCourse
myFavoriteHolidaySpot
  • In python to create variables we use snake case

How python is developed

Numbers

  • We will look into the following built in data types
    • Booleans
    • Integers
    • Floats
  • In python bool type has only two values True, False
    • Lets look at how integers can be converted to bool’s Preview
    • operators that can be bool
      • not
      • and
      • or
  • In python int can hold integer values
  • Lets look at some operator
Operator Description Example Result
+ Addition 5+7 12
Subtraction 7-5 2
* Multiplication 7*5 35
/ Floating point division 7/5 1.4
// Integer division (quotient) 7 //5 1
% Modulus (remainder) 7%5 2
** Exponentiation 2 ** 3 8
  • Operator precedence: Refer Here Preview
  • To convert into integer use int() function Preview
  • How big is integer

Exercise:

  • Value of the following when calculate by python
    • 5 * 2 – 3
    • ( 100 – 5 **3 ) / 5
    • 2 ** 2 + 24 // 4

Spacing in Python

  • We can write
5**3
  • But since Zen of python says readability counts
5 ** 3

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin