Month: May 2021

Learn Python By Example – while Statement

while Statement Import the random module import random Create a variable of the true number of deaths of an event deaths = 6 Create a variable that is denotes if the while loop should keep running running = True while running is True while running: /* Create a variable that randomly create a integer between …

Learn Python By Example – if and if else

if and if else Create a variable with the status of the conflict. 1 if the conflict is active 0 if the conflict is not active unknown if the status of the conflict is unknwon   conflict_active = 1 If the conflict is active print a statement if conflict_active == 1: print(‘The conflict is active.’) …

Learn Python By Example – Add Padding Around String

Try, Except, and Finally Create data /* Create some data */ scores = [23,453,54,235,74,234] Try something that doesn’t work /* Try to: */ try: /* Add a list of integers and a string */ scores + ‘A string of characters.’ /* If you get an error, set the error as ‘e’, */ except Exception as …

Learn Python By Example – Swapping Variable Values

Swapping Variable Values Setup the originally variables and their values one = 1 two = 2 View the original variables ‘one =’, one, ‘two =’, two (‘one =’, 1, ‘two =’, 2) Swap the values one, two = two, one View the swapped values, notice how the values for each variable have changed ‘one =’, …

learn Python By Example – String Operations

String Operations Python 3 has three string types str() is for unicode bytes() is for binary data bytesarray() mutable variable of bytes   Create some simulated text. string = ‘The quick brown fox jumped over the lazy brown bear.’ Capitalize the first letter. string_capitalized = string.capitalize() string_capitalized ‘The quick brown fox jumped over the lazy …

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 …