Day 3 of #100DaysOfCode
Python.
Motivation, Don't Repeat Yourself.

1)Defining a function
def function_name(parameters_if_any):
"""indentation identifies the code block of a function"""
return data_if_any
Parameters: are variables in function definition.
Arguments: are the values put into parameters when functions are called.
Calling a function: function_name(arguments_if_any)
2)Modules
These are the codes written to perform useful tasks. Some modules are already part of standard library and others need to be
installed.

-To import an existing module e.g. math
import math #imports whole math module
-To import only a function
from math import pi #only imports pi
-To install a module:
pip install module_name # to get module name refer pypi.org
Some useful modules
string,re,datetime,math,random,os,multiprocessing,subprocess,socket,email,json,sys,doctest
3)Exception Handling:
try:
#do something
except:
#execute this code if exception/error occurs in try block
finally:
#this block will always be executed.
you can specify which exception to handle in except statement.
-Raising an exception:
exceptions can also be raised using raise statements as
Syntax: raise Exception_type("details")
Fun Fact: you can even pass variables or functions as details.
if its the function that is passed
the function will be called(only if an exception has been caused) and do what it is supposed to do then the exception will be raised.
-raise statement when used in except block can be used to determine the type of exception.
-Assertion:
Syntax: assert expression,arguments
here arguments can be int/str,variables, functions just like raise statement.
It can be used as a sanity checker in code.
4)File Handling:
-Opening a file:
f = open("filename","mode")
modes can be:r,w,a,r+,w+,a+(will write about each in details some other day)
-Reading a file:
f.read(n), if n omitted whole file is read, else n number of bytes. returns a string.
f.readlines(n), if n omitted while file is returned as list with each line as a separate element. else n number of lines in list.
-Writing file:
f.write(string_here)

Good Practice:
try:
f=open("filename")
except:
f.close()
5)Data Structures Dictionary:
Syntax:
dic = {key:value,key:value}
key can be any immutable data type.
value can be any data type.
Retrieving data:
dic[key] gives the corresponding value if the key exists else KeyError.
or
dic.get(key) gives the corresponding value if key exists else None.
dic.keys() returns all dictionary keys, in dict_keys data type.
6)Tuples:
Syntax:
tup = (1,'1')
-are immutable data types. But have a look at the following example
tup=(1,[1,2]) its a valid tuple and you can perform all list operations of list e.g tup[1].append(3) gives
tup=(1,[1,2,3])
-tuples are ordered
-can't increase the number of elements in the tuple.
-slicing is the same as that of the list.

Implemented a web scrapper using Selenium.

Machine Learning:
Completed next 3 sections of Machine Learning Crash Course by Google.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Muhammad Irfan

Muhammad Irfan Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Pythonista92

17 Aug
Hi Reader, I hope you are having a good day and will have a great life from now on.

Day 8 of #100DaysOfCode.

Python
Type 2 of itertools
remember you have to import them using
from itertools import name_of_itertool
1)takewhile
Take values from iterable while the predicate function remains true,once predicate function returns false it will no
longer take values.
example:
lis=[2,4,1,8,6,14]
lis_even=list(takewhile(lambda x:x % 2 == 0,lis))
returns lis_even=[2,4]
2)accumulate
Returns a running total of values in iterable.
lis=[0,1,2,3]
lis_new=list(accumulate(lis))
returns lis_new=[0,1,3,6]
Read 7 tweets
16 Aug
Hi Reader, I hope you are having a good day and will have a great life from now on.

Day 7 of #100DaysOfCode.

Python
1)Recursive Functions:
Every recursive function has a base case that stops it from going into the fourth dimension(that's what I like to think 😅).
Example 1: Image
Recursive functions can be implemented with more than one function. Here's an example for checking even and odd numbers
Example 2: Image
Read 8 tweets
12 Aug
Hey Reader, I hope you are having a good day and will have a great life from now on.

Realized that I messed up the thread for day 4 so gonna tweet that again.
👇
Day 4 of #100DaysOfCode

1) List Comprehension:
is a useful way to create lists when elements obey simple rule.
e.g cubes = [i**3 for i in range(10)]
With conditional
cubes = [i**3 for i in range(10) if i%2==0]
2) String Formatting:
Syntax: 'some string {0} {1} {2}'.format(value1,value2,value3)
e.g.
msg = "{0} is my friend,{0} is my best friend. His nickname is {1} {2}".format('Bob','Tony','-hehe')
format can have any data that needs to be printed out.
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(