📉
Tutorials
  • Computer History
  • Function
    • Finance
      • Calculate
    • Manage Data
    • Date&Time
    • Strings and Character
  • Snippets
    • Web Application
      • Hugo
      • JavaScript
        • Stopwatch using JavaScript?
    • Note
    • Start Project
      • GitHub
      • GitLab
    • Python Programming
      • Strings and Character Data
      • List
      • Dictionaries
    • Data Science
      • Setting Option
      • Get Data
  • Link Center
    • Next Articles
    • Google
    • Excel VBA
    • Python
      • Notebook
    • WebApp
      • Vue.js
    • Finance
    • Project
      • Kids
        • Scratch
      • Finance
        • Plotly.js
        • Portfolio
      • Mini Lab
        • Systems Administration
        • Auto Adjust Image
      • Sending Emails
      • ECS
        • Knowledge Base
        • ระบบผู้เชี่ยวชาญ (Expert System)
        • Check product
        • Compare two SQL databases
      • e-Library
        • Knowledge base
        • การจัดหมวดหมู่ห้องสมุด
        • Temp
      • AppSheet
        • บัญชีรายรับรายจ่าย
      • Weather App
      • COVID-19
  • Tutorials
    • Data Science
      • Data Science IPython notebooks
    • UX & UI
      • 7 กฎการออกแบบ UI
    • Web Scraping
      • Scrape Wikipedia Articles
      • Quick Start
    • GUI
      • pysimple
        • Create a GUI
      • Tkinter
        • Python Tkinter Tutorial
      • PyQt
        • PyQt Tutorial
    • MachineLearning
      • การพัฒนา Chat Bot
      • AI ผู้ช่วยใหม่ในการทำ Customer Segmentation
      • Customer Segmentation
      • ตัดคำภาษาไทย ด้วย PyThaiNLP API
    • Excel & VBA
      • INDEX กับ MATCH
      • รวมสูตร Excel ปี 2020
      • How to Write Code in a Spreadsheet
    • Visualization
      • Bokeh
        • Part I: Getting Started
        • Data visualization
        • Plotting a Line Graph
        • Panel Document
        • Interactive Data Visualization
    • VueJS
      • VueJS - Quick Guide
    • Django
      • Customize the Django Admin
      • พัฒนาเว็บด้วย Django
    • Git
      • วิธีสร้าง SSH Key
      • Git คืออะไร
      • เริ่มต้นใช้งาน Git
      • การใช้งาน Git และ Github
      • รวม 10 คำสั่ง Git
      • GIT Push and Pull
    • Finance
      • Stock Analysis using Pandas (Series)
      • Building Investment AI for fintech
      • Resampling Time Series
      • Python for Finance (Series)
      • Stock Data Analysis (Second Edition)
      • Get Stock Data Using Python
      • Stock Price Trend Analysis
      • Calculate Stock Returns
      • Quantitative Trading
      • Backtrader for Backtesting
      • Binance Python API
      • Pine Script (TradingView)
      • Stocks Analysis with Pandas and Scikit-Learn
      • Yahoo Finance API
      • Sentiment Analysis
      • yfinance Library
      • Stock Data Analysis
      • YAHOO_FIN
      • Algorithmic Trading
    • JavaScript
      • Split a number
      • Callback Function
      • The Best JavaScript Examples
      • File and FileReader
      • JavaScript Tutorial
      • Build Reusable HTML Components
      • Developing JavaScript components
      • JavaScript - Quick Guide
      • JavaScript Style Guide()
      • Beginner's Handbook
      • Date Now
    • Frontend
      • HTML
        • File Path
      • Static Site Generators.
        • Creating a New Theme
    • Flask
      • Flask - Quick Guide
      • Flask Dashboards
        • Black Dashboard
        • Light Blue
        • Flask Dashboard Argon
      • Create Flask App
        • Creating First Application
        • Rendering Pages Using Jinja
      • Jinja Templates
        • Primer on Jinja Templating
        • Jinja Template Document
      • Learning Flask
        • Ep.1 Your first Flask app
        • Ep.2 Flask application structure
        • Ep.3 Serving HTML files
        • Ep.4 Serving static files
        • Ep.5 Jinja template inheritance
        • Ep.6 Jinja template design
        • Ep.7 Working with forms in Flask
        • Ep.8 Generating dynamic URLs in Flask
        • Ep.9 Working with JSON data
        • Ep.23 Deploying Flask to a VM
        • Ep.24 Flask and Docker
        • Ep. 25: uWSGI Introduction
        • Ep. 26 Flask before and after request
        • Ep. 27 uWSGI Decorators
        • Ep. 28 uWSGI Decorators
        • Ep. 29 Flask MethodView
        • Ep. 30 Application factory pattern
      • The Flask Mega-Tutorial
        • Chapter 2: Templates
      • Building Flask Apps
      • Practical Flask tutorial series
      • Compiling SCSS to CSS
      • Flask application structure
    • Database
      • READING FROM DATABASES
      • SQLite
        • Data Management
        • Fast subsets of large datasets
      • Pickle Module
        • How to Persist Objects
      • Python SQL Libraries
        • Create Python apps using SQL Server
    • Python
      • Python vs JavaScript
      • Python Pillow – Adjust Image
      • Python Library for Google Search
      • Python 3 - Quick Guide
      • Regular Expressions
        • Python Regular Expressions
        • Regular Expression (RegEx)
        • Validate ZIP Codes
        • Regular Expression Tutorial
      • Python Turtle
      • Python Beginner's Handbook
      • From Beginner to Pro
      • Standard Library
      • Datetime Tutorial
        • Manipulate Times, Dates, and Time Spans
      • Work With a PDF
      • geeksforgeeks.org
        • Python Tutorial
      • Class
      • Modules
        • Modules List
        • pickle Module
      • Working With Files
        • Open, Read, Append, and Other File Handling
        • File Manipulation
        • Reading & Writing to text files
      • Virtual Environments
        • Virtual Environments made easy
        • Virtual Environmen
        • A Primer
        • for Beginners
      • Functions
        • Function Guide
        • Inner Functions
      • Learning Python
        • Pt. 4 Python Strings
        • Pt. 3 Python Variables
      • Zip Function
      • Iterators
      • Try and Except
        • Exceptions: Introduction
        • Exceptions Handling
        • try and excep
        • Errors and Exceptions
        • Errors & Exceptions
      • Control Flow
      • Lambda Functions
        • Lambda Expression คืออะไร
        • map() Function
      • Date and Time
        • Python datetime()
        • Get Current Date and Time
        • datetime in Python
      • Awesome Python
      • Dictionary
        • Dictionary Comprehension
        • ALL ABOUT DICTIONARIES
        • DefaultDict Type for Handling Missing Keys
        • The Definitive Guide
        • Why Functions Modify Lists and Dictionaries
      • Python Structures
      • Variable & Data Types
      • List
        • Lists Explained
        • List Comprehensions
          • Python List Comprehension
          • List Comprehensions in 5-minutes
          • List Comprehension
        • Python List
      • String
        • Strings and Character Data
        • Splitting, Concatenating, and Joining Strings
      • String Formatting
        • Improved String Formatting Syntax
        • String Formatting Best Practices
        • Remove Space
        • Add Spaces
      • Important basic syntax
      • List all the packages
      • comment
    • Pandas
      • Tutorial (GeeksforGeeks)
      • 10 minutes to pandas
      • Options and settings
      • เริ่มต้น Set Up Kaggle.com
      • Pandas - Quick Guide
      • Cookbook
      • NumPy
        • NumPy Package for Scientific
      • IO tools (text, CSV, …)
      • pandas.concat
      • Excel & Google Sheets
        • A Guide to Excel
        • Quickstart to the Google Sheets
        • Python Excel Tutorial: The Definitive Guide
      • Working With Text Data
        • Quickstart
      • API Reference
      • Groupby
      • DateTime Methods
      • DataFrame
      • sort_values()
      • Pundit: Accessing Data in DataFrames
      • datatable
        • DataFrame: to_json()
        • pydatatable
      • Read and Write Files
      • Data Analysis with Pandas
      • Pandas and Python: Top 10
      • 10 minutes to pandas
      • Getting Started with Pandas in Python
    • Markdown
      • Create Responsive HTML Emails
      • Using Markup Languages with Hugo
    • AngularJS
      • Learn AngularJS
    • CSS
      • The CSS Handbook
      • Box Shadow
      • Image Center
      • The CSS Handbook
      • The CSS Handbook
      • Loading Animation
      • CSS Grid Layout
      • Background Image Size
      • Flexbox
  • Series
    • จาวาสคริปต์เบื้องต้น
      • 1: รู้จักกับจาวาสคริปต์
  • Articles
    • Visualization
      • Dash
        • Introducing Dash
    • Finance
      • PyPortfolioOpt
      • Best Libraries for Finance
      • Detection of price support
      • Portfolio Optimization
      • Python Packages For Finance
    • Django
      • เริ่มต้น Django RestFramework
    • General
      • Heroku คืออะไร
      • How to Crack Passwords
    • Notebook
      • IPython Documentation
      • Importing Notebooks
      • Google Colab for Data Analytics
      • Creating Interactive Dashboards
      • The Definitive Guide
      • A gallery of interesting Jupyter Notebooks
      • Advanced Jupyter Notebooks
      • Converting HTML to Notebook
    • Pandas
      • Pandas_UI
      • Pandas Style API
      • Difference Between two Dataframes
      • 19 Essential Snippets in Pandas
      • Time Series Analysis
      • Selecting Columns in a DataFrame
      • Cleaning Up Currency Data
      • Combine Multiple Excel Worksheets
      • Stylin’ with Pandas
      • Pythonic Data Cleaning
      • Make Excel Faster
      • Reading Excel (xlsx) Files
      • How to use iloc and loc for Indexing
      • The Easiest Data Cleaning Method
    • Python
      • pip install package
      • Automating your daily tasks
      • Convert Speech to Text
      • Tutorial, Project Ideas, and Tips
      • Image Handling and Processing
        • Image Processing Part I
        • Image Processing Part II
        • Image tutorial
        • Image Processing with Numpy
        • Converts PIL Image to Numpy Array
      • Convert Dictionary To JSON
      • JSON Dump
      • Speech-to-Text Model
      • Convert Text to Speech
      • Tips & Tricks
        • Fundamentals for Data Science
        • Best Python Code Examples
        • Top 50 Tips & Tricks
        • 11 Beginner Tips
        • 10 Tips & Tricks
      • Password hashing
      • psutil
      • Lambda Expressions
    • Web Scraping
      • Web Scraping using Python
      • Build a Web Scraper
      • Web Scraping for beginner
      • Beautiful Soup
      • Scrape Websites
      • Python Web Scraping
        • Web Scraping Part 1
        • Web Scraping Part 2
        • Web Scraping Part 3
        • Web Scraping Part 4
      • Web Scraper
    • Frontend
      • Book Online with GitBook
      • Progressive Web App คืออะไร
      • self-host a Hugo web app
  • Examples
    • Django
      • Build a Portfolio App
      • SchoolManagement
    • Flask
      • Flask Stock Visualizer
      • Flask by Example
      • Building Flask Apps
      • Flask 101
    • OpenCV
      • Build a Celebrity Look-Alike
      • Face Detection-OpenCV
    • Python
      • Make Game FLASH CARD
      • Sending emails using Google
      • ตรวจหาภาพซ้ำด้วย Perceptual hashing
        • Sending Emails in Python
      • Deck of Cards
      • Extract Wikipedia Data
      • Convert Python File to EXE
      • Business Machine Learning
      • python-business-analytics
      • Simple Blackjack Game
      • Python Turtle Clock
      • Countdown
      • 3D Animation : Moon Phases
      • Defragmentation Algorithm
      • PDF File
        • จัดการข้อความ และรูป จากไฟล์ PDF ด้วย PDFBox
      • Reading and Generating QR codes
      • Generating Password
        • generate one-time password (OTP)
        • Random Password Generator
        • Generating Strong Password
      • PyQt: Building Calculator
      • List Files in a Directory
      • [Project] qID – โปรแกรมแต่งรูปง่ายๆ เพื่อการอัพลงเว็บ
      • Python and Google Docs to Build Books
      • Tools for Record Linking
      • Create Responsive HTML Email
      • psutil()
      • Transfer Learning for Deep Learning
      • ดึงข้อมูลคุณภาพอากาศประเทศไทย
        • Image Classification
    • Web Scraper
      • Scrape Wikipedia Articles
        • Untitled
      • How Scrape Websites with Python 3
    • Finance
      • Algorithmic Trading for Beginners
      • Parse TradingView Stock
      • Creating a stock price database with MariaDB and python
      • Source Code
        • stocks-list
      • Visualizing with D3
      • Real Time Stock in Excel using Python
      • Create Stock Quote Module
      • The Magic Formula Lost Its Sparkle?
      • Stock Market Analysis
      • Stock Portfolio Analyses Part 1
      • Stock Portfolio Analyses Part 2
      • Build A Dashboard In Python
      • Stock Market Predictions with LSTM
      • Trading example
      • Algorithmic Trading Strategies
      • DOWNLOAD FUNDAMENTALS DATA
      • Algorithmic Trading
      • numfin
      • Financial Machine Learning
      • Algorithm To Predict Stock Direction
      • Interactive Brokers API Code
      • The (Artificially) Intelligent Investor
      • Create Auto-Updating Excel of Stock Market
      • Stock Market Predictions
      • Automate Your Stock Portfolio
      • create an analytics dashboard
      • Bitcoin Price Notifications
      • Portfolio Management
    • WebApp
      • CSS
        • The Best CSS Examples
      • JavaScript
        • Memory Game
      • School Clock
      • Frontend Tutorials & Example
      • Side Menu Bar with sub-menu
      • Create Simple CPU Monitor App
      • Vue.js building a converter app
      • jQuery
        • The Best jQuery Examples
      • Image Slideshow
      • Handle Timezones
      • Text to Speech with Javascript
      • Building Blog for Your Portfolio
      • Responsive Website Layout
      • Maths Homework Generator
  • Books
    • Finance
      • Python for Finance (O'Reilly)
    • Website
      • Hugo
        • Go Bootcamp
        • Hugo in Action.
          • About this MEAP
          • Welcome
          • 1. The JAM stack with Hugo
          • 2. Live in 30 minutes
          • 3. Using Markup for content
          • 4. Content Management with Hugo
          • 5. Custom Pages and Customized Content
          • 6. Structuring web pages
          • A Appendix A.
          • B Appendix B.
          • C Appendix C.
    • Python
      • ภาษาไพธอนเบื้องต้น
      • Python Cheatsheet
        • Python Cheatsheet
      • Beginning Python
      • IPython Cookbook
      • The Quick Python Book
        • Case study
        • Part 1. Starting out
          • 1. About Python
          • 2. Getting started
          • 3. The Quick Python overview
        • Part 2. The essentials
          • 14. Exceptions
          • 13. Reading and writing files
          • 12. Using the filesystem
          • 11. Python programs
          • 10. Modules and scoping rules
          • 9. Functions
          • 8. Control flow
          • 4. The absolute basics
          • 5. Lists, tuples, and sets
          • 6. Strings
          • 7. Dictionaries
        • Part 3. Advanced language features
          • 19. Using Python libraries
          • 18. Packages
          • 17. Data types as objects
          • 16. Regular expressions
          • 15. Classes and OOP
        • Part 4. Working with data
          • Appendix B. Exercise answers
          • Appendix A. Python’s documentation
          • 24. Exploring data
          • 23. Saving data
          • 20. Basic file wrangling
          • 21. Processing data files
          • 22. Data over the network
      • The Hitchhiker’s Guide to Python
      • A Whirlwind Tour of Python
        • 9. Defining Functions
      • Automate the Boring Stuff
        • 4. Lists
        • 5. Dictionaries
        • 12. Web Scraping
        • 13. Excel
        • 14. Google Sheets
        • 15. PDF and Word
        • 16. CSV and JSON
    • IPython
    • Pandas
      • จัดการข้อมูลด้วย pandas เบื้องต้น
      • Pandas Tutorial
  • Link Center
    • Temp
  • เทควันโด
    • รวมเทคนิค
    • Help and Documentation
  • Image
    • Logistics
Powered by GitBook
On this page
  • Intermediate Python Refresher: Tutorial, Project Ideas, and Tips
  • This article is to teach Python beginners and developers some key concepts used in Python that aren't taught from the get-go.
  • How It All Started
  • General Tips
  • Refresher
  • Let’s Start
  • File I/O
  • Error Handling
  • More Data Types
  • List Comprehension and Ternary
  • Iterables vs. Primitives
  • Memoization
  • Lambdas
  • Modules
  • Third Party Modules
  • Advanced Topics (future Python learning)
  • Project Ideas

Was this helpful?

  1. Articles
  2. Python

Tutorial, Project Ideas, and Tips

PreviousConvert Speech to TextNextImage Handling and Processing

Last updated 4 years ago

Was this helpful?

Intermediate Python Refresher: Tutorial, Project Ideas, and Tips

This article is to teach Python beginners and developers some key concepts used in Python that aren't taught from the get-go.

If you can create a quadratics root solver, you'll be able to understand this article. These are some concepts I didn't learn in one day but rather a couple years as I am self-taught.I tried to make this into a video, but the video ended up being 1:45 hours long; I myself would need a lot of motivation to watch a “tutorial” that long, and I prefer articles to get my information since I can pick out relevant details rather than trying to skip sections of a video.

How It All Started

General Tips

These are some tips that are not bound to programming but just life and productivity in general.Know your keyboard shortcuts Know both the program specific ones (browser, explorer, IDE of choice, etc.) and also OS specific ones (e.g. Win + R for run). Know the command lineInstead of doing a calculation by hand or opening an IDE to create and run a script, you can actually execute Python code from the command line. Aside from the common batch functions (e.g. ls, cd), knowing how to use Python from the command line will save you a lot of timeKnow how to GoogleGoogle (or your search engine of choice), is my best friend and should also be yours. It has saved me a lot of time and so it could also save you a lot of time. It can’t do that if you don’t use it or don’t know how to use it. When you Google something, your query needs to be general enough that you can find answers, but also specific enough so that those answers are relevant.Problem Breakdown StrategyThis goes hand in hand with Googling. Suppose you have a problem/project. You need to break it down into smaller parts. You then need to analyze each of these parts and see if they are small enough for you to complete each of them. If not, either your missing some knowledge that you should Google or the part is too big and needs to be broken down again. You keep doing this recursive procedure until your project has been split into solvable parts so that you can complete them and then weave together a project. When I search and find answers through Google, I don’t expect them to be 100% what I need. I usually need to remix them into what I want and that’s what you should also expect: the bare minimum solution that takes you at least one step forward.With these tips stated, you can do a couple of different things next. You can skim the rest of the document and make notes on the snippets of code I feature (what I would do personally), read only the headings, skip to the project ideas section, or stop reading altogether as my tips are so useful.

Refresher

In CS Circles, they bring abou`t the print function and some of its optional parameters but it’s easy to forget about them so here it is again.

>>> # The default parameters for print are sep=' ', and end='\n'
>>> print('4', '21', 2020, sep='/', end='\n---------\n')
4/21/2020
---------

Let’s Start

Input function and String FormattingThe input function has an optional parameter so that it can also act as a prompt and if you are using Python 3.6+, you can make use of f-strings.

name = input('Enter your name: ')
print(f'Hello {name}!')  # modern way of string formatting
# if input='reader', output: Hello reader!

For Loops I want to make clear to you that a for loop, is not a while loop as it is in other languages. In Python, a for loop is an iteration over an iterable object.The range function has three parameters, two of them being optional. Do not write use the range function with an explicit start value of 0 because 0 is the default start value, (unless of course you are modifying the default step value of 1).In this example, I will show you exactly what I mean by “not a while loop” and how a for loop (specifically range) does not add to the temporary value.

# range(start=0, stop, step=1)
# range(5) == range(0, 5) == range(0, 5, 1)

for i in range(5):
    print(i)
    i += 2
# Guess the output. HINT: i += 2does nothing

If you run this code, you’ll notice that the output is increasing by 1 each time even if we are adding 2 to i at every loop. This is because i is set to the next value in range and isn’t actually being increased by one each time. This means that we can actually iterate over all sorts of iterable objects, like lists, without having to use range and indexing.

some_letters = ['a', 'b', 'c', 'd', 'e']
for letter in some_letters:
    # do something
    pass

Here I introduced the keyword pass, this is to avoid errors in otherwise empty blocks.If you do want to keep track of the index as well as the item, you still don’t have to use range, you can use the built-in function enumerate.

for i, letter in enumerate(some_letters, start=0):
    print(f'item at index {i} is {letter}')

You can think of enumerate as turning an iterable into an iterable of pairs (index, item of iterable at index).You can also use the next function to retrieve the next value in an iterable (if there is no next item, an error will be raised).

File I/O

# make sure test.txt exists with text in it

# OLD 
f = open('test.txt')  # note default is mode='r'
# do something with f here
f.close()
with open('test.txt') as f: # NEW; no close() needed
   print(f.read())
   # f.read() moves the "cursor" to the end of the file
   assert not f.read()
   f.seek(0)
   assert f.read()
   # f.read() returns a string now (unless test.txt is empty)

with open('test.txt', 'w') as f:
    # f.read()  ERROR do not do this
    f.write('this is a test\n')  # note there is no end parameter
    f.writelines(['line1\n', 'line2\n'])  # note no auto newline# other modes: a for append, rb for reading-bytes, wb for writing bytes, and r+/w+ for both reading and writing at the same time

If you are curious why the r+/w+ is not the default, think about how a file cannot be opened to be written to if it is being “written” to by another program. If you just need to read a file, it being open in another program means that you won’t be interfering with the other program.

Error Handling

# handling the error
try:
    raise RuntimeWarning('Something could go wrong')
except RuntimeWarning as e:  # as e is optional
    # handle the exception here

# ignoring the error
# old
try:
    raise Exception('BOO')
except Exception: pass

# new
from contextlib import suppress
def ignore_error(exception: Exception): 
    """
    Use three quotes for docstrings or long strings
    """
    # : for type hinting (in a dynamic typed language!) and
    # yes you can pass exceptions and functions as parameters
    with suppress(exception):
        raise exception('BOO')
        print('not printed')

ignore_error(RuntimeError)
print('this gets printed')

By this point if you are following along in PyCharm, you would have seen some squiggly lines, especially under “Exception” in the above code. These squiggly lines help you to avoid syntax errors, follow style guidelines, and bring attention to code that could be doing something you didn’t want it to be doing.

More Data Types

So what are these iterables I keep mentioning? Yes a list is an iterable and so are tuples (which you should already know of).There are also dictionaries, sets and generators (not discussed here). Dictionaries are like hash tables in other languages, because they “hash” the key to store information.

empty_dict = {}  # or dict()
my_dict = {'key': 'value'}
# How to get value from dict
my_dict['a']  # raises KeyError if 'a' not in dictionary
my_dict.get('a', DEFAULT_VALUE)

if 'key' in my_dict:
    val = my_dict['key']
val = my_dict.get('key', None)
if val is not None: pass
with suppress(KeyError):
    val = my_dict['key']

# iterations
for k in my_dict: pass  # or for k in my_dict.keys()
for v in my_dict.values(): pass
for k, v in my_dict.items():
    # since items() generates the items as the iteration happens, 
    #  my_dict cannot be modified in this loop.
    # For modification use tuple(my_dict.items())
    pass

# remove key from dict
del my_dict['key']  # can raise KeyError

# if you want to use the value, use .pop() and define a default 
# value to avoid KeyErrorsmy_dict.pop('key', DEFAULT_VALUE)

# set
empty_set = set()  # {} would initialize an empty dict
my_set = {1, 2, 3}
if 1 in set: pass
# there are many set methods, go check them out yourself
# some include: union, intersect, difference
# you can use + and - as well

Data Structure Usage (Efficiency) The data structure you use is very important to writing good code.use dictionaries if order doesn’t matter + each key has information (value) associated with ituse sets if order doesn’t matter + no values per key (e.g. keeping track of what you have ‘used’ per se)use tuples if you need ordered data but don’t need to modify the data (e.g. coordinates)use lists if you need order and mutability (most flexible)You can’t use sets or dictionaries or sets if you need to keep track of duplicates. That’s because sets and dictionaries hash the keys so that it is super fast (O(1)) to check if a key is in a dictionary. This does mean that you can’t use lists, sets, and generators as keys (but you can definitely use tuples as long as lists are not nested).Dictionaries are also like JSON objects so you can actually use the json module to export them to a JSON file. Note that if you’re using sets as values, they are converted to lists in an exported json file.Miscellaneous FunctionsSometimes you will see functions like

func(*args, **kwargs)
# args = a list of arguments
# kwargs = keyword arguments 
# (in the function it'll be a dictionary)
# *args: list in the function **kwargs: dict in the function
def complex_func(*args, **kwargs):
    pass

def normal_func(a, b, c, sample_param=5):
   pass

sample_args = {'sample_param': 3}
args = [0, 1, 2]

complex_func(1, 2, 3, test='true')  # how you'd call it
complex_func(*args, **sample_args)  # also works on normal functions
normal_func(*args, **sample_args)

List Comprehension and Ternary

One of the most beautiful parts of Python is list comprehensions; one liners to create lists.

# example: input is space separated integers
integers = [int(x) for x in input.split()]
# split(sep=' ', maxsplit=-1), -1 means no limit
no_negatives = [x for x in integers if x > 0]  # only if
positives = [x if x > 0 else -x for x in integers]  # if and else
back_to_str = ' '.join((str(x) for x in integers))
# items in the list to join need to be of type str
print(integers)

# this next case demonstrates the ternary operator _ if _ else _
print('list is', 'not empty' if integers else 'empty')

You can also use list comprehensions to create dictionaries and sets

set_example = {x for x in range(3)}
dict_example = {x: x for x in range(3)}
generator_example = (x for x in range(3))  # use sparingly

The third example is a generator. There are some use cases for it, so do your research before using them as they are an advanced topic not for this article.

Iterables vs. Primitives

There is one very important distinction between primitive variables and iterable variables. For example.

a = 5
b = a
a = 6
print(a == b)  # false
# vs.
a = [1, 2, 3]
b = a
c = [1, 2, 4]
a[2] = 4
print(a == b == c)  # true
print(a is b)  # true; same refrence
print(a is c)  # false

This is especially important when dealing with nested iterables with how you create nested iterables and also copy them. Try out these examples yourself.

lols = [[0] for i in range(3)] # [0] is created 3 times
lols[0][0] = 5
print(lols)  # [[5], [0], [0]]
# vs.
a = [[0]]
lols = a * 3  # same as lols = [[0] * 3]
lols[0][0] = 5
print(lols)  # [[5], [5], [5]]

Copying IterablesTo make a shallow copy, use .copy(). BUT, note that for any nested iterables, only the reference is copied, not the actual nested list. That’s why it’s called a shallow copy. To deepcopy, we can use the copy module.

new_copy = lols.copy()  # I prefer this over using [:]
reversed_list = lols[::-1]
# I rarely use this^ as reversed() and .reverse() exist
new_copy[0][0] = 6  # lols == [[6], [6], [6]]
assert lols == new_copy and not lols is new_copy

from copy import deepcopy

new_copy = deepcopy(list_of_lists)
new_copy = list_of_lists
new_copy[0][0] = 4  # [[4], [4], [4]] because 3x of the same list
assert lols != new_copy and lols is not new_copy

Memoization

Memoization is the caching of function return results in order to speed up repetitive calculations. An example would be the recursive implementation of the Fibonacci sequence.

def memo(func):  # remove print statements in a practical setting
    cache = {}

    def _helper(x): # constrained to one param (in this case)
        # you could have multiple params (x, y, ...) and then 
        # cache using tuple keys
        if x not in cache:
            print('not in cache')
            cache[x] = func(x)
        else:
            print('in cache')
        return cache[x]

    return _helper


@memo  # square = memo(square) <-- what it means
def square(x):
    return x * x

for i in range(3):
    square(i), square(i)  # second one uses cached result

An exercise is to make an addition function (a, b) that uses memoization.

Lambdas

Usually used in place of a function parameter if the calculation is short. For example, sorting.

['aa', 'Bb', 'Cc', 'dD'].sort(key=lambda string: string.upper())
[('a', 1), ('b', 0)].sort(key=lambda pair: pair[1])
sorted([('a', 1), ('b', 0)], key=lambda pair: pair[1])
max([('a', 1), ('b', 0)], key=lambda pair: pair[1])  # and min

Modules

Modules play a big part in projects you will do. Some built-in ones are os, shutil, copy, glob, and threading.os

import os
os.mkdir()  # to make a NEW dir
os.chdir()  # choose a current working dir
os.getcwd()  # get current working dir
os.path.exists()
os.rename()
os.remove()  # for existing files only
os.rmdir()
os.getenv('key')  # gets an environmental variable

# use the shutil module for directories with sub directoriese

Environmental variablesSpecify project secrets in a

.env

file

# in .env
KEY=VALUE

# in *.py
with open('.env') as f:
    for line in f.read().splitlines():
        k, v = line.split('=', 1)
        os.environ[k] = v

globUsed for getting a list of files/folders

from glob import glob

print(glob('*.py'))  # get all .py files in cwd, * is a wildcard
# exercise: find out how to get all .py files in cwd + its subdirs

Third Party Modules

You need to use `pip install module_name` to install modules.Some modules that are common are requests, beautifulsoup4, PIL, and flask. If you’re working on a big project, you’ll probably end up using 3rd party modules.

Advanced Topics (future Python learning)

ClassesI did not cover classes because that is more about OOP than Python programming and the use cases for classes are very small. One thing you should know when you are learning classes is __slots__ property, so do search that up on your own.GeneratorsAgain this is an advanced topic and learning about it now will only lead to confusion, its best to learn this on your own or in a practical setting.DecoratorsI covered only the basics of decorators. There are decorators used by lots of other 3rd party libraries and different use cases (e.g. timing functions) so I suggest you do your own research on them as well.git and git workflowThis is very important when your collaborating with others or are working for a company. Git is a versioning tool used so that mistakes don’t hurt you, and for letting you work on multiple features at the same time.other builtin modulesSuch as itertools, threading, multiprocessing, and more.

Project Ideas

You can find a list of project ideas below:

PRACTICAL PROJECT IDEAS

Your own website/portfolio (no bootstrap, use raw HTML/CSS/JS + flask backend). Use Heroku (free) to host it [Hard]

A program that applies a blur to an image you select [Easy]
    (use tkinter/SG so that you don't have to hard code the filename every time someone uses the program)

A program that sets your desktop wallpaper and cycles through a folder of images, [Okay]
    change the wallpaper every x (you decide) seconds.

Web Scraping & Data Parsing [Hard]
    Go to https://www.cia.gov/library/publications/the-world-factbook/rankorder/2004rank.html and use inspect elements,
    Your job is to parse this web page and filter the data so that only the data with the latest "date of information"
    remains. Output should be order-preserved and in a CSV file (filename is arbitrary)
    To make it easier (not recommended), you can assume the latest date of info is 2017 EST.
    To make it even easier (not recommended) just preserve the order and output all of the data in a CSV
    hint: use requests, bs4, and the csv module
    Another challenge is to download the data as a txt file and convert that text file to a CSV file using Python
    hint: you may find ' '.join(iterable) useful

Using an API [Okay - Hard]
    Spotify Reference
    You'll have to get a Spotify API key
    See https://developer.spotify.com/documentation/web-api/reference/search/search/ and
    make a program that lets users search for tracks using the Spotify API (you return a list of tracks)
    See https://elijahlopez.herokuapp.com/search-album-art/ for a demo but with album art
    You just need to spit out a list of tracks
    To make this harder, you will sort the tracks by popularity (you'll have to search the documentation yourself)
    To make this even harder, you can add a sort by artists feature and MORE
    # requests, base64 module,

Try to send an email to yourself (your email -> your email) using Python [Easy - Hard]
    To make this harder, try to also send an attachment
    module: at least smptlib

You can try to make your own key logger [Okay - Hard]
    At first just append the contents to a .log or .txt file
        Use the 'a' open type to append to bottom.
        HARDER: YOU WILL INCLUDE COPIED AND PASTED ITEMS WITH custom syntax
        # (e.g. USER_COPIED("")), (e.g. USER_PASTED(""))

Make your own soundboard with your own sounds [Hard because tedious]
    You PySimpleGUI for the GUI (I recommend but you don't have to)

    - Let your soundboard include support for different "packs"
    - Specify a design/package guide for other users to create use their sounds on your soundboard
    default: your default pack
    PACKS_DIR/pack_name/sound_name/sound .mp3/ogg
    # pygame, playsound


Make your own game using pygame (OOP)


Turn one of these projects into an executable [Okay but Tedious]
    - use sys.exit() instead of quit()
    pyinstaller, or cx_freeze

Make your own discord bot [variable difficulty] (async)

Email me or comment for bigger programming (not specifically Python) project ideas

Comment if you have any questions.Thanks for reading and good luck to your learning journey.

I learned Python basics through and then proceeded to improve/test my problem solving skills. I did this by doing questions which you can find (among other contest problems) at . Other sites to improve your algorithmic problem solving skills include and (although this is mainly for interview preparations).While I was doing this, I was coding with the default IDLE editor! I did this for 3 months and then I found out about which has a slight learning curve but is loads better in terms of features and at improving productivity. Nowadays, I use both PyCharm and .I personally have an entire folder dedicated to snippets of code I could use in the future, so I suggest you also do that and maybe even add some of the code snippets in this article in there so that you can read your own examples instead of Googling them or coming back to this article.

I’m including this because if you search “how to read files in python” on Google, you are given which teaches it the old way and not the modern approach.

ThreadingI have already written some threading example for you to look at . Just follow the instructions in the gist.

Reference :

CS Circles,
CCC
DMOJ
HackerRank
LeetCode
PyCharm
Visual Studio Code
this
here
https://hackernoon.com/intermediate-python-refresher-tutorial-project-ideas-and-tips-i28s320p
May 12th 2020