Skip to content

Instantly share code, notes, and snippets.

@zahra-ove
Last active May 3, 2024 08:05
Show Gist options
  • Save zahra-ove/b9f4689fc9cac1b45aa917b0491e8a4a to your computer and use it in GitHub Desktop.
Save zahra-ove/b9f4689fc9cac1b45aa917b0491e8a4a to your computer and use it in GitHub Desktop.
  1. all python files should have .py extension.
  2. interesting note: in this code, * is printed 10 time:
print("*" * 10)
  1. there is two different version for python:
  • python2
  • python3

python2 is supported until 2020.

  1. linting --> analyzing your code for potential errors or problems. pylint is one of popular tools for linting in python.

  2. boolean values : True , False (python is a case sensitive language)

  3. 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.

  4. everything in python is an object.

  5. Falsy values in python:

    • "" (empty string)
    • 0
    • None (None is an object which represent the absence of value)
  6. by default in python all functions return None (if that function explicitly does not have any return, then it returns None)

  7. None is an object which represents the absence of a value.

  8. function with variable number of arguments:

def sample_function(*args):
    //
  1. 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}
  1. sample use of keyword arguments:
def increment(number, by):
  return number + by
  
  
print(increment(2, by=1))

-----------------------Data Types ----------------------------------

  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

------------------------ working with strings ---------------------------

  1. check if a substring exists in a string or not:
course = "python programming"
print("zahra" in course)  # False
print("zahra" not in course) # True
  1. 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

------------------------ control flow ---------------------------

  1. 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment