Python#1 Language Fundamentals

6 Reasons why Python Programming is so popular
List of topics to be covered here

Core python
  1. Language fundamentals
  2. operators
  3. Input and output statements
  4. flow controls
  5. strings
  6. list data structure
  7. Tuple data structure
  8. Set data structure
  9. Dictionary data structure
  10. Functions
  11. Modules
  12. Packages
  13. Exception handling
  14. File handling
Advanced python
  1. OOPs
  2. Ragular expression
  3. Muti-threading
  4. Python Database connectivity (PDBC)
Language Fundamentals
Python is released in 1991 and it is named after a popular BBC TV show of 1969-1974 "Monty Python's Circus". Although interpreter has built in compiler, python is an interpreted language.

Python has some advantages over the other existing languages
  1. Platform independent: means write the code once and run it anywhere
  2. Portability: there is no need to change the code if moving from one OS environment to the other OS environment
  3. Python program can be used in other languages too.
  4. If there is any speed problem in python then in that case we can use C/C++ to implement that part of the code and use it in python to improve its performance but this way we may loose platform independency.
  5. python is a dynamically typed programming language. C,C++ and Java are statically typed programming language because we declare a variable before using it. While, in python we can change the type of variable at run time.
  6. Python has much meaningful syntax like
  7. a,b,c,d,e = 10,20,30,40,50 //valid in python but not in c/c++/java
    type(a) // to check the type of var a
    
    def f1(): print("Good Evening") //defining a function f1()
    f1() //output: Good Evening
    
    x=10 if a>b else 20 //valid in python
    
    for i in range(10):
       print(i)
    
Python has borrowed the following concepts
  • scripting from perl and shell script
  • modular feature from modula-3
  • OOPs from C++
  • functional programming from C
  • syntax from C and ABC language
Limitations of python
  • Performance
  • Mobile development

Flavors of python
  • Anaconda python: to handle big data
  • RubyPython: for ruby applications
  • PyPy: for high performance
  • IronPython: for C# applications
  • JPython: to work with Java applications
  • CPython: to work with C applications
  • Stackless: for Multithreading in python
Python versions
  • Python 1.0 in 1994
  • Python 2.0 in 2000
  • Python 3.0 in 2008
  • Python 3.6.3 in 2016
Although It is true for almost all the languages that the new version provide support for the the old version but it is not true for the python because python3 doesn't give support for python2 from 2020.
print "hello" //valid in python2 but not in python3
print("hello") // for python3
Long data type is not present in Python3 but is was there in Python2.

Hence it is always recommended to use Python3.

Identifiers
Any name in the python is an identifier. It can be either variable name, Class name or method name.

Rules to define Identifiers:
  1. Alphabet symbol both upper case and lower case
  2. number from 0 - 9
  3. underscore ( _ )
  4. Identifiers should not start with a digit
  5. Identifiers are case sensitive 
  6. keywords cannot be identifiers
  7. No length limit on python identifiers
  8. If the identifier starts with the underscore symbol then it is private.(_x)
  9. If the identifier starts with the double underscore then it is strongly private.(__x)
  10. If the identifier starts with the double underscore and ends with the double underscore then it is a language specific identifiers.(__x__)
cash = 10 //valid
ca$h = 10 //not valid

total123 = 10 //valid
123total = 10 //not valid

total = 10 //valid
TOTAL = 10 //valid

if = 10 //not valid
def = 20 //not valid

_x //way to declare private variable
__x //way to declare strongly private variable
__add__ //language specific identifier
Reserved Words in Python3
There are total of 33 reserved keywords in python3 while this number was 53 for Java.
  1. True, False, None
  2. and, or, not, is
  3. if, else, elif
  4. while, for, break, continue, return, in, yield
  5. try, except, finally, raise, assert
  6. import, from, as, class, def, pass, global, nonlocal, lambda, del, with
To see the list of keywords in python use the below code
import keyword
keyword.kwlist

Data Types
  1. bytes
  2. str
  3. bool
  4. int
  5. float
  6. complex 
  7. bytearray
  8. range
  9. list
  10. tuple
  11. set
  12. frozenset
  13. dict
  14. None (like void in C or null in java)
x = 10 //here x is of type int, because x is going to store an int data
y = 10.123 //here y is of type float
z = 10+2j //here z is of type complex
Lets look at some of the python in-build function so that we better understand the coming topics
  1. print(): to print the string on the console
  2. type(): to check the type of variable
  3. id(): to find the address of object
a = 10
id(a) //it gives the address of the object a targeting to
Wait a minute, where does that object come from?
Actually everything from variable, number, to method in python are by default internally an Object.
Hence, in a = 10, 10 is an object and we are creating reference to that object.

int
int data can be entered in 4 ways
  1. Decimal
  2. Binary
  3. Octal
  4. Hexadecimal
a = 1111 //decimal way
print(a) //1111

a = 0b1111 or 0B1111 //binary way
print(a) //15

a = -0B1111
print(a) //-15

a = 777 
print(a) //777

a = 0o777 or 0O777 //octal way
print(a) 

a = 0O1111
print(a) //585

a = 0xFace or 0XFace //hexadecimal way
print(a) //64204

a = 0xBeef
print(a) //48879

a = 0xBeer //invalid (think why so?)
One thing to notice here is that whatever way data is being entered print function always prints output in decimal format. But we will print them in their respective format later in this tutorial.

So now question comes what will be the range of int or other data types, like in C, char data type holds -128 to 127?
There is no range concept in python because every number in python is an Object and there is no fixed size for object in any language. So object can contain any value.

Base Conversion: Here some of the function is discussed to convert from different int base to other.
  1. bin() : from any base to binary
  2. oct() : from any base to octal
  3. hex() : from any base to hexadecimal
These functions takes only int argument (form decimal, octal, hex, and bin) and returns int value.
bin(15) // 0b1111
bin(0o777) // 0b111111111
bin(0x123) // 0b1001000110100

oct(786) // 0o1422
oct(0xface) // 0o175316
oct(0B11111) // 0o37

hex(10) // 0xa
hex(16789) // 0x4195
Float
for float data type only decimal representation is allowed in python
f = 123.456 // allowed
f = 0x123.456 // not allowed
f = 0o123.456 // not allowed

f = 1.2e3
print(f) // output: 1200.0

f = 1.2e100 
print(f) //output: 1.2e+100
Complex
  • Every complex number in python should be written as x = a+bj
  • Remember j should be there not i.
  • Real part a: can be decimal, oct, hex, bin but imaginary part b: can only be decimal.
  • a and b can be int or float
x = 10+20j
print(x) //10+20j
type(x) //complex

x=10+j20 //syntax error

a = 10+20j
b= 20+30j
print(a+b) //30+50j
print(a*b) //-400+700j
print(a.real) //10.0 why float not int (think): because in python real and imag part of a complex number is treated as float by default
print(a.imag) //20.0
Bool
This data type is used to hold the result of logical operation
  1. True: is internally treated as 1
  2. False: is internally treated as 0
x1 = 10
x2 = 20
c = x1>x2
print(c) //False

//Things to remember
//	True + True = 2
//	True + False = 1
//	True / True = 1.0 //flaoting point result: because in python division operator is only defined for floating values.
//	True / False //invalid: divisible by zero
str
string data type is used to work with the string: anything enclosed between single or double quotes 
//way to write an string
a = asif //here asif is not an string
a = 'asif' //string
a = "asif" //string

b = 2 //here 2 is an int
b = '2' //here 2 is an string, now we can say that there is no char data type in python.
b = "2" //here 2 is an string

//Always remember that if we print an string
//it will always be printed in single quotes by python
a ="shoaib"
print(a) //'shoaib'

a = 'asif'
print(a) //'asif'
It is time to see how to write multiple line string in python
a = "asif nazeer" //allowed
a = "asif
nazeer" //not allowed this way: use triple single quotes or triple double quotes

a = """asif
nazeer""" //allowed
a = '''asif
nazeer''' //allowed
print(a) //'asif
	//nazeer'
Slice operator
This operator is introduced in python to provide flexibility to the programmer.
syntax: var[begin:end:step] returns substring from begin index to end-1 index.
s = "Durga"
//-5	-4	-3	-2	-1
//D	u	r	g	a
//0	1	2	3	4

print(s[0]) //D
//now print function is omitted
s[2] //'r'
s[-3] //'r'
s[-1] //'a'
s[10] //error (think why?)
s[1:4] //'urg'
s[2:4] //'ur'
s[1:] //'urga', if end value is not given python assumes this value as end value bydefaul
s[3:] //'ga'
s[:4] //'durg', if begin value is not given python assumes this values as begin by default
s[:] //'Durga', because python assumes end and begin values
s[-1:-4] //nothing in the output, because begin index is higher than the end
s[-4:-1] //'urg'
s[100] //error
s[1:100] //'urga' (why?) lets understand
//var[begin:end:step] here by default step value is 1
//in the prev example end is 100 and begin is 1 
//so it will print from 1 to end-1 with step=1
//if there is no character upto given end index 
//then it will return upto what index is available

s = "aligarh Muslim University"
s[1:10] //'ligarh Mu'
s[1:10:2] //'lgr u'

s = "AMU"
print(s*3) //'AMUAMUAMU'
print(len(s)) //3
Type casting or type conversion
some function are listed here to convert from one type of data to the other
  • int() : from any data type to int type
  • float() : from any data type to float type
  • complex() : from any data to complex type
  • str() : from any data type to string type
  • bool() : from any data type to bool type
print(int(123.456)) //123

//now print is omitted
int(10+20j) //can't convert complex to int
int(True) //1
int(False) //0
int("10") //10
int("10.5") //not allowed, because python will think that the data is being converted
//from string to int but inside string its a float value
//Remeber string containing int decimal is only allowed

int("1111") //1111
int("0B1111") //not allowed
//for float(): only int decimal and float inside string is allowed
float(10) //10.0
float(10+20j) //not allowed
float(True) //1.0
float(False) //0.0
float("10") //10.0 
float("10.5") //10.5
float("ten") //not allowed
float("0b1111") //not allowed
//complex(): cannot take the second argument if first one is string
complex(10) //10+0j
complex(10.5) //10.5+0j
complex(True) //1+0j
complex(False) //0j, remember when both x,y are zeros
complex("10") //10+0j 
complex("10.5") //10.5+0j
complex("ten") //not allowed
complex("0b1111") //not allowed
complex(10,20) //10+20j
complex(True,False) //1+0j
complex(10,20.5) //10+20.5j
complex("10","20") //not allowed because complex function cannot take the second argument
//first argument is string
bool(0) //False
bool(10) //True
bool(0.0) //False
bool(0.8) //True
bool(2.8) //True
bool(10+20j) //True
bool(0+0j) //False
bool('') //False
boo(' ') //True, because there is an space
//remember for bool funcion only 0+0j and empty strings are False and everything else is true
//str() : takes anything as input: no restriction
str(10+20j) //'10+20j'
str(True) //'1'
str(False) //'0'
str(0) //'0'
str(20.0) //'20.0'
Immutable Vs Fundamental Data Types
Thanks for visiting here...

Comments