Everything know about the Python

Everything know about the Python

Python is a general purpose and high level programming language which introduced by Guido van Rossum in 1991.

It is used for :

  • Web Development
  • Data Science
  • Software Development
  • System Scripting

Features :

  • General-Purpose Programming Language
  • Interpreted
  • Object-Oriented
  • Dynamic Typed
  • Cross Platform Compatibity
  • Rich Library Support
  • Open Source

print() function :

a built-in function which used for print anything.

syntax : print(value, .... , sep = ' ', end='/n', file= sys.stdout, flush=False)

print('Hello World')      #Hello World        
print('Hello','World',sep='-')    #Hello-World           
#bydefault end='\n'

print('Hello', )     #Hello
print('World')   #World

#if changed then, 
print('Hello', end=' ' )    
print('World')       #Hello World                     

Data Types

  • Basic Data Types : int, float, complex, boolen, string
  • Container-Based Data Types : list, set, tuple, dict
  • User Defined Based : class

#Basic Data Types
a = 10   #int
b = 2.5 #float
c = True  #boolean
c' = False #boolean
d = 'LinkedIn' #str (string)
e = 3+4j  #complex           
# Container Based 
a = [1,2,3,'Hi']  #List
b = (1,2,3,'python') #tuple
c = {'name' : 'raj', 'age': 21}  #dict
d = { 1,2,3, 'ok'}   #set        
#User-Defined
class Myclass:
  print('This is my class')        

Type Conversion

Type Conversion is used for convert a datatype from another.

for example, if you want convert int to float then :

a = int(2.3) #2

b = float(4) #4.0

c = int('10') #10

Keywords

Keywords are a reserved word which has some special meanings.

For Example - as, if , elif, break , continue etc.

Python has 33 keywords.

import keyword
print(keyword.kwlist) #show all keyword list        

Variables

Variable - like a container which used to store values

for example, a = 3

Rules For Assign Variable

  • Don't start with numbers
  • Always starts with letters or _
  • Don't use keywords name ( as , if , for )

Literal : Literals are the values are assigned in variable from different formats.

for example ,

  • arithmetic literal

a = '0b1010' #binary

a = '0x12c' #hexa

a = '0o312' #octa

a = 1.5e10 #float

  • string literal

a = 'python'

a ="python"

a = ''' python '''

a = u'\uf1243\ugh6784\' #emoji

a = r' a \n b'

  • boolean literal

a = True + 4 #5

b = False + 10 #10

  • Special Literal

a = None #nothing in a ( used for variable declaration)

Operators

  • Arithmetic Operators
  • Relational Operator
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Assignments Operator
  • Identity Operators

# Arithmetic Operators ( + , -  , * , / , ** , % , // )
print(3+4)  # 7
print(3-4)  #-1
print(3*4)   #12
print(3/4) #.75
print(3**4)  #81
print(21//4) #1
print(21%4)   #5        
#Relational Operators ( > , < ,>=, <= , ==, !=)
print(5>3)   #true
print(5<3)  #false
print(5>=3)  #true
print(5<=3) #false
print(5==3)  #false
print(5!=3) #true        
#Logical Operators ( and, or , not)
a = True
b = False
print(a and b)  #False
print(a or b)    # true
print(not a) #false         
#Bitwise Operators ( & , | , ~ , <<, >>) 
#bitwise and
print( 3 & 5) # 011 and 101 = 001 # --- (1)
#bitwise or
print( 3 | 5)  # 011 or 101 = 111 # --- (7)'
#bitwise left shift
print(3<<5)  # 3*(2^5) = 32
# bitwise right shift
print(12>>2) #12/(2^2) = 3
# bitwise not 
print(~3)   #-3        
#Identity Operators ( is , is not)
a = 'Hello'
b = 'world'
print( a is b)  #true
print(a is not b)  #false        
#Membership Operators ( in, not in)
print('D' in 'Delhi')  #true
print('d' not in 'Delhi')  #true        
#Assignments Operators ( =, +=,-=,*=, /=)
a = 3  
a+ = 3
print(a) #6
a *= 3
print(a) #18
a /= 3
print(a) #3        

Input Function

Input function is used for take input from user.

for example , name = input('enater your name);

age = int(input('enter your age'))


if-else statements

if-else staements used for conditional logic building for example if condition true that's doing this else condition false then do this.

# Login System if email=='datatrick@gmail.com' and password=='py123': print('welcome') elif email=='datatrick@gfmail.com' and password!='py123': print('Incorrect Password') password = input('Enter Correct password') if password == 'py123': print('welcome') else: print('still incorrect') elif email != 'datatrick@gfmail.com' and password =='py123': print('Incorrect Email') else: print('Incorrct Password or Email')


Loops

Loops are used for iteration and repeat your task again and again until your condition is not false.

There have two types of loops :

  • While Loop
  • For Loop

While Loop

while i<11:
   print(i)
   i+=1
# Output
1
2
3
4
5
6
7
8
9
10        

For Loop

For Loop iterate for range function and sequnce data like string or list .

for i in arnge(1,11):
  print(i)

1
2
3
4
5
6
7
8
9
10

for i in ([1,,2,3,4]):
print(i)  

1
2
3
4
          

Break, Continue and Pass Statement

Break Statement is used for terminate loop for specified condition.

Continue statment is used for skip the loop for specified condition.

Pass statement is used for not doing (empty loop) anything in loop , function or classes.

# break

for i in range(1,11):
 if i==5:
   break
print(i)

1
2
3
4
5

# continue
for i in range(1,11):
 if i==5:
   continue
print(i)

1
2
3
4
6
7
8
9
10

for i in range(1,11):
   pass        

Built-In Function in Python

  • print
  • input
  • type
  • int
  • abs
  • pow
  • min/max
  • round
  • divmod
  • bin/oct/hex
  • id
  • len
  • sum
  • help

Built-in Modules

  • random
  • os
  • math
  • time

String

String is asequence of characters.

For Example , "python is popular language" is string.

create string

a = 'python is object-oriented'

a = "python is high level"

a = '''johny,johy,yes papa,

Eating Sugar, No Papa !''' #multi-line string

a = str('python learn easy')

accesing substring from string

a = 'python is best'

#first char

a[0] #p

#last char

a[-1] #t

#substring

a[0:6] #python

a[0:8:2] #ptoi

Editing and deleting string

Editing in String is not possible because string are immutable ( not changeable)

Deleting -

a = 'python is best'

del a # delete string

but , if you do this

del a[-1] #this not possible

String Operation

a = 'Hello'

b = 'World'

a + b # HelloWorld

a * 3 # HelloWorldHelloWorldHelloWorld

'H' in a #true

'h not in a #true

'Hello'>'World' #false lexicography dict based ( which character after called big)

'Hello'<'World' #true

We know that - ' ' ---->False ; 'python' ---->True

'Hello' and 'World' #World

'Hello' or 'World' #Hello

Functions in String

  • len
  • min/max
  • sorted
  • capitalize
  • title
  • sort
  • upper
  • lower
  • swapcase
  • count
  • find/index
  • endswith/startswith
  • format
  • isalnum/isalpha/isdigit/isidentifier
  • split
  • join
  • replace
  • strip

Python Data Structures

  • List
  • Tuple
  • Set
  • Dict

List - List is a mutable data type which used to store heterogeneous values.

#Create a list

a = [1,2,3,'hi']

a = list(1,2,3,'hi')

a = [] #empty list

#update a list

a[0] = 100

a #[100,2,3,'hi']

#add value in list

  • append : add item into last position
  • extend : add multiple elements into list
  • insert : add item at specified index

#delete list

  • clear : remove list all values
  • remove : remove the first item eith specified value
  • del : delete list
  • pop : remove element at specified position

#list operations

l1 = [1,2,3]

l2 = [4,5,6]

l1 + l2 #[1,2,3,4,5,6]

l1 *3 #[1,2,3,1,2,3,1,2,3]

3 in l1 #yes

5 in l1 #no

#List function

  • len
  • type
  • sorted
  • min/max

Tuple : It is a immutable data type which used to store heterogeneous values.

#create tuple -

a = (1,2,3)

a = tuple(1,2,3)

a = (1,) #single element tuple

#edit tuple : it's not possible because it is immutable.

#delete tuple :

  • del
  • remove
  • pop
  • clear

#Tuple Operations

t1 = (1,2,3)

t2 = (4,5,6)

t1 + t2 #(1,2,3,4,5,6)

t * 2 #(1,2,3,1,2,3)

2 in t1 #yes

8 in t2 #no

#Tuple functions

  • min/max
  • len
  • type
  • sorted

Sets

Rules for a set -

  • does not allow duplicates value
  • sets have no indexing
  • sets elements are not mutable
  • sets also a mutable

#create set

a = {1,2,3}

a = set(3,2,1)

a = {} #it doesn't create empty set set

a = set() #create empty empty set

#add a value in set

a.insert(5)

#Operation in set

s1 = {1,2,3}

s2 = {4,5,6}

s1 + s2 #{1,2,3,4,5,6}

#function

len(s1) #3

type(s1) #set

sorted(s1)

s1.union(s2) #{1,2,3,4,5,6}

s1.intersection(s2) #{}

Dict

Dictionary is a mutable data type which used to store value as key-value pairs.


#create dict

d1 = {'name' : 'raj', 'age' : 21}

d1 = dict(name' : 'raj', 'age' : 21)

d1 ={} #empty dict

#access keys

d1.keys()

#access values

d1.values()

#using for loop

for i in d1:

print(i,d[i])

#add a value in dict -

d1['gender']='male'

#update value in dict

d1['name'] = 'raj singh'

#delete

del d1['gender']

del d1 #whole dict delete

d1.clear() #{}

d1.pop() #last key-value










To view or add a comment, sign in

More articles by Rahul Rathour

  • Docker

    Docker is an open -source platform that allows you to automate the deployment and management of applications within…

  • NLP Pipeline

    Set of the steps in which used to implement a NLP software called NLP Pipeline. Following are the steps : Data…

  • Django Basic-2-Advance

    What is framework ? A framework is a set of conceptual structure and guidelines, used to build something useful. A…

  • What is Devin AI ?

    Devin is not just a program; it's a groundbreaking AI that acts as a software engineer, capable of coding, debugging…

  • TRANSFORMERS

    A Transformer Model is a type of deep learning model that was introduced in 2017. The model was wirst described in 2017…

  • NLP

    NLP stands for Natural Language Processing. It is a subfield of linguistics, computer science and artificial…

  • GEMINI AI MODEL

    Gemini is a new and powerful artificial intelligence model from Google that can understand not just text but also…

Insights from the community

Others also viewed

Explore topics