Day: May 28, 2021

Learn Python By Example – String Indexing

String Indexing Create a string string = ‘Strings are defined as ordered collections of characters.’ Print the entire string string[:] ‘Strings are defined as ordered collections of characters.’ Print the first three characters string[0:3] ‘Str’ Print the first three characters string[:3] ‘Str’ Print the last three characters string[-3:] ‘rs.’ Print the third to fifth character …

Learn Python By Example – String Formatting

String Formatting Import the sys module import sys Print a string with 1 digit and one string. ‘This is %d %s bird!’ % (1, ‘dead’) ‘This is 1 dead bird!’ Print a dictionary based string ‘%(number)d more %(food)s’ % {‘number’ : 1, ‘food’ : ‘burger’} ‘1 more burger’ Print a string about my laptop. ‘My …

learn Python By Example – Sort A List Of Strings By Length

Sort A List Of Strings By Length Create a list of names commander_names = [“Alan Brooke”, “George Marshall”, “Frank Jack Fletcher”, “Conrad Helfrich”, “Albert Kesselring”] Sort Alphabetically By Length To complete the sort, we will combine two operations: lambda x: len(x), which returns the length of each string. sorted(), which sorts a list.   /* …

Learn Python By Example – Sort A List Of Names By Last Name

Sort A List Of Names By Last Name Create a list of names commander_names = [“Alan Brooke”, “George Marshall”, “Frank Jack Fletcher”, “Conrad Helfrich”, “Albert Kesselring”] Sort Alphabetically By Last Name To complete the sort, we will combine three operations: lambda x: x.split(” “), which is a function that takes a string x and breaks it up …

Learn Python By Example – Set The Color Of A Matplotlib Plot

Set The Color Of A Matplotlib Plot Import numpy and matplotlib.pyplot %matplotlib inline import numpy as np import matplotlib.pyplot as plt Create some simulated data. n = 100 r = 2 * np.random.rand(n) theta = 2 * np.pi * np.random.rand(n) area = 200 * r**2 * np.random.rand(n) colors = theta Create a scatterplot using the …

Learn Python By Example – Selecting Items In A List With Filters

Selecting Items In A List With Filters /* Create an list of items denoting the number of soldiers in each regiment, view the list */ regimentSize = (5345, 6436, 3453, 2352, 5212, 6232, 2124, 3425, 1200, 1000, 1211); regimentSize (5345, 6436, 3453, 2352, 5212, 6232, 2124, 3425, 1200, 1000, 1211) One-line Method This line of …

Learn Python By Example – Select Random Element From A List

Select Random Element From A List Preliminaries from random import choice Create List /* Make a list of crew members */ crew_members = [‘Steve’, ‘Stacy’, ‘Miller’, ‘Chris’, ‘Bill’, ‘Jack’] Select Random Item From List /* Choose a random crew member */ choice(crew_members) ‘Stacy’ Python Example for Beginners Special 95% discount 2000+ Applied Machine Learning & …

Learn Python By Example – Scheduling Jobs In The Future

Scheduling Jobs In The Future /* Import required modules */ import sched import time /* setup the scheduler with our time settings */ s = sched.scheduler(time.time, time.sleep) /* Create a function we want to run in the future. */ def print_time(): print(“Executive Order 66”) /* Create a function for the delay */ def print_some_times(): /* …

Learn Python By Example – Recursive Functions

Recursive Functions Simple factorial print(5*4*3*2*1) 120 Recursive function The tell-tale sign of a recursive function is a function that calls itself /* Create a function inputing n, that, */ def factorial(n): /* if n is less than or equal to 1, */ if n <= 1: /* return n, */ return n /* if not, …

learn Python By Example – Queues And Stacks

Queues And Stacks Preliminaries from collections import deque Make A Queue /* Option 1: Make a queue */ queue = deque(range(10)) /* Option 2: Make a queue that, if full, discards any item at the opposite end to where you added an item. */ queue = deque(range(10), maxlen=10) Manipulate Queue /* Append an item to …