Python First Bl

·

4 min read

                                                                             **PYTHON
                                                                          -------------**

Top 10 Python Frameworks for Web Development:

  1. Django
  2. Pyramid
  3. CherryPy
  4. Flask
  5. Bottle
  6. WebPy
  7. TurboGears
  8. CubicWeb
  9. AIOHTTP
  10. Dash by Plotly

Sebastianraschka:-https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

Super Quick Return Exercise:- Q:-1)Write a function called speak pig that returns 'oink'. Yup, that's it. Ans:-def speak_pig(): return 'oink'

Q:-2)Write a function called generate_evens that returns a list of the even nos. between 1 and 50(not including 50). [2,4,6....all the way up to 48]. Use a loop OR list comprehension.

Creating a Touch Function Thanks to Zachary for originally posting this on the discussion board!

Rather than typing the long/ugly command to create a new file using PowerShell, you can set up a custom function to mimic the touch command you'll see me use on my mac.

Creating a temporary function If you were to run the following in your terminal:

function touch {New-Item -ItemType File -Name ($args[0])} it will create a temporary function (temp meaning while that instance of PowerShell is open) that will allow you to type:

touch filename

Creating a "permanent" function

To make this a "permanent" change it just needs to be added to the PowerShell profile found at:

C:\Users\USERSNAMEHERE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 Note that you need to alter the above path to reflect your actual username.

First, run:

Test-Path $PROFILE to check if a profile file already exists.

If True, then navigate to the above location and copy and paste into the function line. Restart PowerShell.

If False, then enter the below command to create a new empty profile: (ONLY DO IF NO PROFILE IS FOUND AS THE -force FLAG WILL OVERWRITE ANY EXISTING PROFILE)

New-Item -Path $PROFILE -Type File -force

At this point navigate to the above profile location and paste in the function. Restart PowerShell.

How to generate random nos:-


->import random ->random.randint(0,2) =>from random inport randint =>number = randint(1, 10)

*lower()

Loops:-


->for char in "hello": print(char)

h e l l o

Controlled Exit:-

The keyword break gives us the ability to exit out of while loops whenever we want: while True: command = input("Type 'exit' to exit: ") if (command == "exit"): break *We can also use it to end for loops early.

List Methods:-

1)append():->Add an item to the end of the list. It takes exactly one argument. e.g.:-data.append(7, 9) #Gives error

2)extends():->Add to the end of a list all values passed to extend

3)insert():->Insert an item at a given position.

4)clear():->Removes all items from the list

5)pop():->Remove the item at the given position in the list, and return it. If no index is specified, remove and return the last item in the list.

6)remove():->Remove the first item from the list whose value is x. Throws a ValueError if the item is not found.

7)index():->Returns the index of the specified item in the list. Can specify start and end.

8)reverse():->Reverse the elements of the list (in-place)

9)join(): ->Technically a String method that takes an iterable argument. ->concatenates (combines) a copy of the base string between each item of the iterable.

Slicing:-

Make new lists using slices of the old list. Syntax:-some_list[start:end:step] ->If you enter a negative number, it will start the slice that many backs from the end

Q:- When do you need to swap? Ans:-Shuffling, Sorting, Algorithm

Nested List:-

->A list inside another list is called a nested list. ->It is also called a multi-dimension list. ->List can contain any kind of element, even other lists.

Applications:-

1)Complex data structures - matrices 2)Game Boards / Mazes 3)Rows and Columns for Visualizations, tabulation, and grouping of data.

Recap:

->Lists are fundamental data structures for ordered information. ->Lists can include any type, even other lists. ->Slices are quite useful when making copies of lists.

Dictionaries:-


->Another approach is to use the dict function. You assign values to keys by passing in keys and values separated by an =. ->dict.items() returns a list of tuples. ->dict.keys() returns a list of keys. ->dict.values() returns a list of values.

Dictionary Methods:-


1)clear():->Clears all the keys and values in a dictionary.

2)copy():->Makes a copy of a dictionary.

3)fromkeys():->Creates key-value pairs from comma separated values.

4)get():->Retrieves a key in an object and returns None instead of a KeyError if the key does not exist.

5)pop():->Takes a single argument corresponding to a key and removes the key-value pair from the dictionary. Returns the value corresponding to the key that was removed.

6)popitem():->Removes a random key in a dictionary. It takes no arguments.

7)update():->Update keys and values in a dictionary with another set of key-value pairs.

Dictionary Comprehension:-


Syntax:->{_:__ for in } ->Iterates over keys by default. ->To iterate over keys and values using .items()