Python Fundamentals

Python Variable

Python Variable is unique, let’s see

but first you should see this article Introduction To Python

Python Variable

Variable is a container {Place-Holder} in the memory, just cutting some space, and then we assign the value {Literal} to that space

python variable get some space on memory and value get assigned to it

the simple way to do that is :

name = 'Ahmed'

int age
age = 20

print("My name is : " + name + "\n" + "My age is : " + age)
My name is : Ahmed
My age is : 20

Python Variable Naming rules

  • Start with letter or underscore
  • Only alpha-numeric characters and underscores
  • Case-sensitive (age isn’t the same as Age)

Python Variable Scope ( Local – Global )

When we declare a variable it’s global then we are able to call it anywhere we want

but if we declared that variable inside function we can’t call him out of i

also if we declared it outside and assigned the value inside, the value stays inside.

def my_func(): 
  d = 3     # this is a local variable
  print(d)  # this will work

print(d)      # that won't work
a = b = c = 5     # Multi Assign

Python Variable Constants

Constants is variables with values can not be changed so it also named one-time assignment values

like they stored in one file or module or class and we are calling from another.

Python Literal ( Data Types )

Types of values assigned to variables are :

  • Numeric
  • Strings
  • Lists
  • Boolean

and there are sub-types and another types too, try them :

a = 15    # Integer
b = 5.5   # Float
c = 15j   # Complex

a = 0b1010   # Binary
c = 0o310    # Oct
d = 0x12c    # Hex
a = 'C'                      # Char
b = 'Ahmed'                  # String
u = "\u00dcnic\u00f6de"      # uni code
r = "raw \n string"          # raw
days = ["saturday", "sunday", "monday"]          #list
sorting = (1, 2, 3)                              #tuple
chairs = {'1':'clean', '2':'good', '3':'bad'}    #dictionary
vowels = {'a', 'e', 'i' , 'o', 'u'}              #set
Ahmed_came = True
Mona_came = False

Python Type Conversion ( Functions )

There is two types of conversion : implicit and explicit, anyway :

  1. int(X[,default]) : X >> Integer , default value in case x is a string
  2. float(X) : X >> Float-Point
  3. complex(real[,img]) : X >> Real , img >> Imaginary
  4. str(X) : X >> String representation
  5. tuple(L) : L >> Tuple
  6. list(L) : L >> List
  7. set(L) : L >> Set
  8. dict(L) : L >> Dictionary
  9. chr(X) : X >> Character

Python Thinking

If we are going to write a program about the zoo we have :

so we will plan for 3 major classes ( fishes, birds and animals )

each class will contain attributes and methods class can do, attributes like name, color and weight

and methods like run, jump, eat, swim or fly then

on a paper i’ll write down methods tree and variables with scope caring and then, i’m ready to go coding..

keep up with us

Write a Comment

Your email address will not be published. Required fields are marked *