-
Maker [Guido Van Rossum] Date [1989]
To list all the keyword in pyton
import keyword
print(keyword.kwlist)
Python features
-
Platform indipendent
open source
python use 4 spaces as indentation
# this will give indentation error
print("Hello world")
print("unexpedted indent")
-
Interpreted language don't need to compile and run.
Comments
-
single line # use this
multiline """ this is multiline comment in python """
Typecasting
-
int(value) , float(value)
know what type of data type our value is
x = int(input("enter the value: "))
print(type(3))
Output_(formating)
-
use end in print statement to remove ̊
you can format any value using .format on string
use sep to put seperation between print statement
print("Hello world" , end='')
print("value ${:.2f}".format(value))
print("hello","world", sep=' ')
value = 10
print("value %d" %value)
Variables && Operators
-
variable can hold different types of value {because python is
dynamically typed}
we can assign multiple variable with different values too
global vs local variable
use global keyword to to use global variable inside a function
use in or not in to find variable in array
Ternary operator [value] if [true] else [not_(true)]
value = 10
def modify_f():
global value
value = "another_value" #this will give error without using global
keyword
print(value)
def f():
print(value) #will use global value because we aren't modifing any value
variable inside function
x = y = c = 100
print(x , y , c , sep=' ')
x1 , y2, c2 = 1 , "python" , 2.0f
different values too
# we can assign multiple variable with
y = [10 , 20 , 30 , 40]
x = 20
if (x in y):
print("it is available")
else:
print("nope")
Type casting
-
float() , int() , str()
input_value = int(input("enter your age: "))
Object Reference In PYTHON
-
python use pointer and create new object when we create a variable
-
Removing Variable using del keyword
x = 10 #created an object with value 10 and pointed to it usig x
y = x # both pointing at same object
y = 20, x = 20
# now 10 object is garbage and will be clear
del x
Condition
-
math case
-
The pass statement in Python is a placeholder that does nothing when
executed. It is used to keep code blocks valid where a statement is
required but no logic is needed yet. Examples situations where pass
is used are empty functions, classes, loops or conditional blocks.
-
you can use else condition with loop if it doesn't throw execption
or break
while i > 10:
print(f"{i}")
else:
print("Nothing")
def fun():
pass
fun() # Call the function
math number:
case 1:
print("one")
case _:
print("another")
String
-
""" hello world """ multiline comment but when s = """ hello world
""" , it becomes multiine string
string slicing
s = "hello world"
print(s[0:5])
Methods on strings
-isupper , islower, isalpha, isalnum
-
upper(), lower()
-
strip() remove whitespaces
-
rstrip() remove any trailing character
-
replace("[char]" , "[with]")
-
find() find first occurence
-
count() return count
-
capitalize()
-
center()
-
index() , find()
-
endswith()
-
swapcase()
str = "hello world!"
print(str.strip()) #removes whitespace
print(str.rstrip("!") #remove ! from the sentence
print(str.center(40))
print(str.split(" ")) # returns list of seperate value