- all python files should have
.py
extension. - interesting note: in this code,
*
is printed 10 time:
print("*" * 10)
- there is two different version for python:
- python2
- python3
python2 is supported until 2020.
-
linting
--> analyzing your code for potential errors or problems.pylint
is one of popular tools for linting in python. -
boolean values :
True
,False
(python is a case sensitive language) -
there is two type of division:
/
and//
In Python, the single forward slash/
performs regular division and returns a floating-point number, while the double forward slash//
performs integer division and returns the result as a whole number by rounding down. -
everything in python is an object.
-
Falsy values in python:
- "" (empty string)
- 0
- None (None is an object which represent the absence of value)
-
by default in python all functions return None (if that function explicitly does not have any return, then it returns None)
-
None
is an object which represents the absence of a value. -
function with variable number of arguments:
def sample_function(*args):
//
- passing multiple keyword arguments:
def insert_product(**product):
print(product['id'])
print(product['name'])
print(product['category_id'])
print(insert_product(id=1, name='watch', category_id=3)) # output: {'id':1, 'name': 'watch', 'category_id':3}
- sample use of
keyword arguments
:
def increment(number, by):
return number + by
print(increment(2, by=1))
- primitive types:
there is three types of built-in primitive types in python.- numbers
-- integers --> like: 2
-- floats --> like: 2.5
-- complex numbers --> like: 1 + 2j - booleans
- strings
- numbers
- check if a substring exists in a string or not:
course = "python programming"
print("zahra" in course) # False
print("zahra" not in course) # True
- to slice strings:
course = "python programming"
print(course[0]) #p
print(course[-1]) #g
print(course[0:3]) #pyt
print(course[0:]) #python programming
print(course[1:]) #ython programming
print(course[:]) #python programming
print(course[::-1]) #gnimmargorp nohtyp --> string is reversed
- ternary operator:
this two piece of code are equal:
age = 22
if age >= 18:
message = "Eligible"
else:
message = "Not Eligible"
print(message)
message = "Eligible" if age >= 18 else "Not Eligible"
print(message)