Tag Archives: Python tutorials

Learn Python By Example – Iterate Over Multiple Lists Simultaneously

Iterate Over Multiple Lists Simultaneously Create Two Lists names = [‘James’, ‘Bob’, ‘Sarah’, ‘Marco’, ‘Nancy’, ‘Sally’] ages = [42, 13, 14, 25, 63, 23] Iterate Over Both Lists At Once for name, age in zip(names, ages): print(name, age) James 42 Bob 13 Sarah 14 Marco 25 Nancy 63 Sally 23   Python Example for Beginners …

Learn Python By Example – Indexing And Slicing NumPy Arrays

Indexing And Slicing NumPy Arrays Slicing Arrays Explanation Of Broadcasting Unlike many other data types, slicing an array into a new variable means that any chances to that new variable are broadcasted to the original variable. Put other way, a slice is a hotlink to the original array variable, not a separate and independent copy …

Learn Python By Example – How To Use Default Dictionary

How To Use Default Dictionary Preliminaries import collections Create A DefaultDict Default Dicts work just like regular dictionaries, except a key is called that doesn’t have a value, a default value (note: value, not key) is supplied. /* Create a defaultdict with the default value of 0 (int’s default value is 0) */ arrests = …

Learn Python By Example – Generating Random Numbers With NumPy

Generating Random Numbers With NumPy Import Numpy import numpy as np Generate A Random Number From The Normal Distribution np.random.normal() 0.5661104974399703 Generate Four Random Numbers From The Normal Distribution np.random.normal(size=4) array([-1.03175853, 1.2867365 , -0.23560103, -1.05225393]) Generate Four Random Numbers From The Uniform Distribution np.random.uniform(size=4) array([ 0.00193123, 0.51932356, 0.87656884, 0.33684494]) Generate Four Random Integers Between 1 …

Learn Python By Example – Functions Vs. Generators

Functions Vs. Generators Create A Function /* Create a function that */ def function(names): /* For each name in a list of names */ for name in names: /* Returns the name */ return name /* Create a variable of that function */ students = function([‘Abe’, ‘Bob’, ‘Christina’, ‘Derek’, ‘Eleanor’]) /* Run the function */ …

Learn Python By Example – Formatting Numbers

Formatting Numbers Create A Long Number annual_revenue = 9282904.9282872782 Format Number /* Format rounded to two decimal places */ format(annual_revenue, ‘0.2f’) ‘9282904.93’ /* Format with commas and rounded to one decimal place */ format(annual_revenue, ‘0,.1f’) ‘9,282,904.9’ /* Format as scientific notation */ format(annual_revenue, ‘e’) ‘9.282905e+06’ /* Format as scientific notation rounded to two deciminals */ …

Learn Python By Example – Flatten Lists Of Lists

Flatten Lists Of Lists Create A List Of Lists /* Create a list containing three lists of names */ list_of_lists = [[‘Amy’,’Betty’,’Cathryn’,’Dana’], [‘Elizabeth’,’Fay’,’Gora’], [‘Heidi’,’Jane’,’Kayley’]] Flatten The Lists Of Lists Into A Single List /* For each element in list_of_lists, take each element in the list */ flattened_list = [i for row in list_of_lists for i …

Learn Python By Example – Exiting A Loop

Exiting A Loop Create A List /* Create a list: */ armies = [‘Red Army’, ‘Blue Army’, ‘Green Army’] Breaking Out Of A For Loop for army in armies: print(army) if army == ‘Blue Army’: print(‘Blue Army Found! Stopping.’) break Red Army Blue Army Blue Army Found! Stopping. Notice that the loop stopped after the …

Learn Python By Example – Display Scientific Notation As Floats

Display Scientific Notation As Floats Create Values /* Create a numbers in scientific notation */ value_scientific_notation = 6.32000000e-03 /* Create a vector of numbers in scientific notation */ vector_scientific_notation = [6.32000000e-03, 1.80000000e+01, 2.31000000e+00, 0.00000000e+00, 5.38000000e-01, 6.57500000e+00, 6.52000000e+01, 4.09000000e+00, 1.00000000e+00, 2.96000000e+02, 1.53000000e+01, 3.96900000e+02, 4.98000000e+00] Display Values As Floats /* Display value as a float */ ‘{:f}’.format(value_scientific_notation) …

learn Python By Example – Dictionary Basics

Dictionary Basics Basics Not sequences, but mappings. That is, stored by key, not relative position. Dictionaries are mutable.   Build a dictionary via brackets unef_org = {‘name’ : ‘UNEF’, ‘staff’ : 32, ‘url’ : ‘http://unef.org’} View the variable unef_org {‘name’: ‘UNEF’, ‘staff’: 32, ‘url’: ‘http://unef.org’} Build a dict via keys who_org = {} who_org[‘name’] = …