Skip to content

Openacademyedu/Python-QuickNotes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 

Repository files navigation

Python QuickNotes

Python QuickNotes help you learn python.

Follow the quick notes to learn python programming.

1. Variable Assignment

Note:

  • Variable names are case sensitive i.e both var and VAR are different variables.
  • Variable names must start with a letter or an underscore. Eg: _var
  • Variable names can contain a number in between but not at the start. Eg: var1
  • Variable names must not use spaces in between

1.1 Single Assignment

Code:

x = 10 # int
z = 5.5 # float
firstName = "Alexa" # str
is_female = True # bool

1.2 Multiple Assignment

Code:

name, age, weight, is_male = ("John", 22, 65.4, True)

Description:

We can assign multiple variables at a single time. The above code is equivalent to -

name = "John"

age = 22

weight = 65.4

is_male = True

2. Basic Maths Operations

Code:

add = x + y # addition
sub = x - y # substraction
div = x / y # division
mod = x % y # modulus
expo = x ** y # exponent

3. Type

Note:

  • All classes and metaclasses including object are subclasses of object.
  • All classes and metaclasses including type are instances of type.
  • All objects including object are instances of object.

Code:

issubclass(type, object) # True
issubclass(object, object) # True
issubclass(object, type)  # False
isinstance(object, type) # True
isinstance(type, type) # True
isinstance(type, object) # True
isinstance(object, object) # True

4. Casting

Code:

x = str(x) # convert to string
y = int(y) # convert to integer
z = float(y) # convert to float

5. String

5.1 Concatenation

Code:

name = "Alexa"
age = 18

print("Hello, My name is " + name + " and I am " + str(age) + " years old.")

5.2 Argument by position

Code:

name, age = ("Alexa", 18)

print("My name is {name} and I am {age} years old.".format(name=name, age=age))

Description:

We can position the arguments inside the { } and then call the .format() method.

5.3 F-Strings

Code:

print(f"My name is {name} and I am {age} years old.")

5.4 String Methods

Code:

s = "hello world"

Operations: s.capitalize() s.upper() s.lower() s.swapcase() len(s) s.replace(old, new, count) s.count(substring) s.startswith(substring) s.endswith(string) s.split() s.find(substring) s.isalnum() s.isalpha() s.isnumeric()

6. List

Note:

  • List is a collection which is ordered and changeable.
  • Allow duplicate members.

6.1 Create List

Code:

fruits = ["Apples", "Oranges", "Mangos", "Pears"] or fruits = list(("Apples", "Oranges", "Mangos", "Pears"))

6.2 List Methods

Operations: fruits.append(element) fruits.remove(element) fruits.insert(position, element) fruits.pop(position) fruits.reverse() fruits.sort() fruits.sort(reverse=True)

7. Tuple

Note:

  • Tuple is a collection which is ordered and unchangeable.
  • Allow duplicate members.

Code:

fruits = ("Apples",)

Description: If you are assigning a single value inside the tuple please use , otherwise without , the type(fruits) return <class 'str'> instead of <class 'tuple'>

8. Sets

Note:

  • A set is a collection of unordered and unindexed.
  • No duplicate members.

Code:

fruits = {"Apples", "Oranges", "Mangos"}

Operations: fruits.add(element) fruits.remove(element) fruits.clear() fruits.update(element) fruits.pop()

9. Dictionary

Note:

  • A dictionary is a collection which is unordered, changeable and indexed.
  • No duplicate members.

9.1 Create Dictionary

Code:

person = {
     'first_name': "John",
     'last_name': "Nash",
     'age' = 22
     }

9.2 Common Operations

Operations: person.get(key) person.items() person.keys() person.values() person.pop(key) person.clear() person.copy() len(person)

9.3 Merge Dictionaries

Code:

x1 = {'first_name' : 'John', 'last_name' : 'Doe'}
y1 = {'age' : 31, 'gender' : 'male'}

merge_x1_y1_dict = {**x1, **y1}

Description: We can merge two or more dictionaries using ** operator as shown in above code. In case if the key finds similar in two or more dictionaries, then the value of the key will be updated with the dictionay defined last.

10. Lambda Functions

Code:

getSum = lambda num1, num2: num1 + num2
print(getSum(10, 3))

Description: A Lambda function ( lambda <arguments> : <expression> ) can take as many number of arguments, but can only have one expression.

11. Function Argument Unpacking

11.1 List/Tuple

Code:

def about(name, age, country):
    print(f"My name is {name} and I am {age} years old. I live in {country}.")
    
about_data = ["John Doe", 22, "USA"]

about(*about_data)

Description: In case your data is in a list or tuple then you can fetch it using * operator i.e. about(*about_data) instead of typing about(about_data[0], about_data[1], about_data[2]) where about_data is the variable assigned to your list.

11.2. Dictionary

Code:

def about(name, age, country):
    print(f"My name is {name} and I am {age} years old. I live in {country}")
    
about_data = {'name': "John Doe", 'age': 22, 'country': "USA"}

about(**about_data)

Description: In case you have to parse your data (inside a dictionay) then you just have to use ** operator in place of single * operator. Note that in case of using dictionay the arguments must match exactly with the dictionay keys.

12. If/Else Statements

Code:

if (condition):
   statement
else:
   statement

13. Operators

13.1 Comparision Operators

Operators: == != > < >= <==

13.2 Logical Operators

Operators: and or not

13.3 Membership Operators

Code:

name =  "Alexa"
first_name = ["John", "Harry", "Alexa"]
if name in first_name:
   print(f'{name} in first_name.')
elif name not in first_name:
   print(f'{name} not in first_name.')

Operators: in not in

13.4 Identity Operators

Operators: is is not

14. Loops

Note:

  • A loop is used for iterating over a set of statements.

14.1 Simple Loop

Code:

first_name = ["Alexa", "John", "Harry"]

for name in first_name:
    print(f'{name}')

14.2 Loop with Break

Code:

first_name = ["Alexa", "John", "Harry"]

for name in first_name:
    if name == "John":
       break
    print(f'{name}')

14.3 Loop with Continue

Code:

first_name = ["Alexa", "John", "Harry"]

for name in first_name:
    if name == "John":
       continue
    print(f'{name}')

14.4 Range

Code:

for i in range(0, 10):
    print(f'{i}')

14.5 While Loop

Code:

count = 0
while count <= 10:
  print(f'{count}')
  count += 1  # count = count + 1

Stay Tuned for learning more...

Download OpenAcademy Android App for learning resources in Data Science and Machine Learning

Download Here

About

Python QuickNotes help you learn python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages