0% found this document useful (0 votes)
69 views

Red: What You'd Actually Put in Python File: Common Differences Between Python and MATLAB, Ways To Approach Python

This document provides an introduction and overview of key concepts in Python programming including: 1. Common differences between Python and MATLAB such as comment syntax, loops, and shell equivalents. 2. Explanations of variables and types, lists, arithmetic operators, string formatting, string operations, conditions, and loops in Python. 3. Descriptions of functions, reading/writing text files, classes and objects, dictionaries, modules and packages in Python.

Uploaded by

Testing Gmail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Red: What You'd Actually Put in Python File: Common Differences Between Python and MATLAB, Ways To Approach Python

This document provides an introduction and overview of key concepts in Python programming including: 1. Common differences between Python and MATLAB such as comment syntax, loops, and shell equivalents. 2. Explanations of variables and types, lists, arithmetic operators, string formatting, string operations, conditions, and loops in Python. 3. Descriptions of functions, reading/writing text files, classes and objects, dictionaries, modules and packages in Python.

Uploaded by

Testing Gmail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Python Programming Notes

Created by: Eddie Krasinski


Black: Usual Notes
Red: What you’d actually put in Python file

Introduction

Here are some notes that I’ve been taking over the past few weeks as I learn Python
using learnpython.org. I recommend going through this site as well, as they give example
problems for each of the sections listed below. My background in programming lies in C and
MATLAB, and unlike these functional programs, Python is object oriented, which will be hard to
grasp as first. In functional programming like C and MATLAB, every single variable you use must
be defined beforehand, but in Python, variables can be created on the spot without prior
definition (mainly in loops), as long as it corresponds to some sort of data (string, values) which
follows the object’s declaration. You’ll see what I mean as you go through these notes. You
could still define all variables beforehand, as you would in MATLAB, but that’s not the best use
of Python. To access all of Python’s built in functions, visit https://docs.python.org/3/library/.

Common Differences between Python and MATLAB, ways to approach Python


o In MATLAB %% is used for comments, in Python, # is used
o Don’t need to use “end” in loops in Python, does this automatically with spacing
▪ Put a colon after each line containing a loop/Boolean statement
o Spacing is very important in loops, functions, classes created in Python, use tabs
to avoid trouble
o MATLAB’s command window= Python’s shell
o Any file ending with .py= A MATLAB Script
o DataFrames in Python are kind of like an Excel table, rows and columns of data
o /n used for new line
o Input() is used to call an element from the user
o / is used to continue current line on the next line (… in Matlab)

Variables and Types


Strings can be added to form a new object, such as
H=”hello”
W= “world”
helloworld= H + “” + W

Can assign multiple variables by separating with a comma


a,b=3,4
print(a,b)

cannot mix strings with numbers, ex


one = 1
helloworld= h + one + W
Lists
.append adds an object to a list in order
Position 0 is actually the first character
For x in mylist:
Print(x)
#this prints out everything in the list

Can also sort through lists like:


A=[1,1,2,3,6,4,8]
B=[2,45,6,3,4]
C=[A*B for element in A for element2 in B if element==element2]
To access certain entries in a list, use brackets:
D=[A[0], A[4:6]]

Arithmetic Operators
%: modulo operator, returns the remainder after division, 11%3 returns 2
** 2: The same as squared
** 3: The same as cubed
Can multiply strings: “hello”*10 gives 10 iterations of hello
Can join lists with addition operator (but can’t mix values with strings)
Can multiply lists, list*3= listlistlist

String Formatting
Print(“hello %s!” % name)
%s can step in for strings and lists
Print(“%s is %d years old” % (name, age))

%d is for integers
%f is for floating point
%.number of digitsf: floating point with specific number of placeholders
%x/%x: Hex representation (integer lowercase/uppercase)

String Operations
Len(string) gives # of characters inside string
.index(“o”) gives place of character, in this case the letter o
String[3:7] gives characters between 3rd and 7th place
String[3:7:2] gives every other character between 3rd and 7th place
String[::-1] returns the string but backwards
.upper converts all characters of string to uppercase
.lower converts all characters of string to lowercase
.startswith(“object”) returns true if string starts with that object, false if not
.endswith(“object”) same as above
.split(somecharacter) creates a list with multiple entries, split at the character chosen
“**”.join(somelistofstrings) will print string1**string2**string2

Conditions
If, and, or is used (if blah and blah then blah)
Is True or is False used
Elif… else # always end line containing condition with colon
== #equate things for Boolean condition
< or > or <= or >= #greater or less than for Boolean condition
In checks if specified object lies within iterable object container
is does not match values of variables, it checks instances, therefore different than ==
Not operator inverses following Boolean phrase

Loops
Don’t need to put an end on for loops (unlike MATLAB)
Range(3,6) returns a new list of 3,4,5
Xrange(3,6) returns an iterator of 3,4,5
While loop is normal (same as C and MATLAB)
!= is not equal to
Count+=1 is the same as count=count+1
Break used to exit for or while loop
Continue used to skip current block and return to for or while loop
Else can be used as a loop(Unlike C/MATLAB), if break inside for loop, else part is
skipped, else part continued even with continue statement
Can access certain points in a list using iterations, such as:
For i in mylist:
If mylist[i]==2:
Print(“This element is equal to 2”)

Functions
Called using def myfunction(args) (myfunction can be any name)
Use return under def to return a value, string, to the caller
To call the function, after defining, just put myfunction(args) in same script
Can return strings like print statements “%s is a good fit for functions” % benefit

Reading, opening, writing to Text Files


Always convert objects to strings before writing to text files. Always close the text file
once you are done editing or reading it. Lastly, if you have a file with the same name, and you
write into the file with “w”, Python will eliminate the original data on that file and replace it
with whatever you are appending to the file.

with open('file_to_save.txt', 'w') as open_file:


open_file.write('A string to write')
or
open_file = open('file_to_save.txt', 'w')
open_file.write('A string to write')
open_file.close()

Classes and Objects


Classes are basically just a more efficient encapsulation of variables and functions
Class Myclass:
Variable=”blah”

def function(args):
return or print or whatever
myobjectx=MyClass()

To obtain the variable within Myclass (variable is just the name of your variable, can be
anything)
myobjectx.variable

Can introduce new variable like this as well


Myobjecty.variable=”whatever”

To access the function within a class (remember, function is just the name of whatever
function you created)
Myobjectx.function()

Can add to variables to objects/lists later in code with variable[“addthis”]==6 #in an if


statement later, this can be used to check if addthis is equal to 6

But what if I want to import a Python file into another, but I don’t want to execute the
variables/functions/classes inside this file until I run the entire program?
Use the __main__ declaration, this ensures that variables, functions, classes etc. are
only run when you hit “Run Module”, not when importing.

Ex.
If __name__==__main__
Function 1
Variable1
Class1
Function2
#none of these objects above will be run while importing this file into another
Dictionaries
Accessed using keys instead of indexes
Phonebook={}
Phonebook[“John”]=9385466788 would print out as {‘John’:9385466788}
Could also look like salaries[name]=salary
Can also be initialized like
Phonebook{ “John”: 9385466788}

Can be iterated over, but does not keep the order of the values stored in it, to iterate in
pairs:
For name, numbers in phonebook.items():
Print(“Phone number of %s is %f” % (name,number))

To delete an index can do either of these


Del phonebook[“John”] or phonebook.pop(“John”)
Can access certain portions of a dictionary using list comprehensions (covered later).
Say you have a dictionary like this
actor = {"name": "John Cleese", "rank": "awesome"}
last_name=actor[“name”].split()[1]
#this assigns the actors last name to the object last_name

Modules and Packages


Import command used to import module (think of modules like other scripts) e.g.
mygame/game.py
Import game

Can also import a function directly using the from command


From game import draw_game

Can import every object within a module using import*


E.g. from game import*

Can use custom import names, useful if you have two modules with similar names
If visual_mode:
Import draw_visual as draw
Else:
Import draw_textual as draw

To tell the Python interpreter where to look for modules, there are two options,
PYTHONPATH and sys.path.append (same as file path in MATLAB)
E.g. PYTHONPATH=/foo python game.py or sys.path.append(“/foo”)

For a complete list of built-in modules: https://docs.python.org/3/library/


E.g. urllib module
Import urllib
Urllib.urlopen()

The dir function can be called to see all functions within a module
Dir(urllib)

The help function can be used to see what that function inside the specific module does,
in this case, what the urlopen function does within the urllib function
E.g. help(urllib.urlopen)

Packages are namespaces that contain modules and more packages, like a directory but
With a twist. Each package in Python must have __init__.py: inside, so the directory
recognizes it as a package, and therefore can be called properly. Packages, modules, classes,
functions are a sort of “hierarchy” within Python to increase efficiency.

If we create a package called “foo”, and create a module inside “foo” named “bar”, we
can call it to the mainspace in one of two ways. “foo” must also contain __init__.py: within it so
Python directory recognizes it as a module.
1. Import foo.bar
2. From foo import bar
__init__.py can be used along with __all__ to choose which modules to export, while keeping
other modules internal like such:
__init__.py:

__all__=[“bar”]

Numpy Arrays
Easier to handle, more efficient than lists, gives you more arithmetic capabilities (useful
for data sets)

Height=[12, 14, 12, 15]


Weight=[200,185,202,145]
Import numpy as np #np could be anything you want
Np_height=np.array(Height)
Np_weight=np.array(weight)
Print(type(np_height))

With numpy array, can perform element wise calculations

Can also subset numpy arrays, for example


Bmi>23
Bmi[bmi>23] #this will print all bmis greater than 23
Pandas Basics
One way to create a dataframe is through dictionaries

dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"],


"capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"],
"area": [8.516, 17.10, 3.286, 9.597, 1.221],
"population": [200.4, 143.5, 1252, 1357, 52.98] }
import pandas as pd
brics = pd.DataFrame(dict)
print(brics)

Automatically indexes with values 0 to end number, can change index “ticker” by such
Brics.index=[“BR”, “RU”, “IN”, “CH”, “SA”] #remember, without specifying index and
append will automatically go to first entry

Another way to access a DataFrame is to use .read_csv


Import pandas as pd
Cars=pd.read_csv(‘cars.csv’)

Indexing: A single bracket will output a Panda Series, double bracket will output Panda
DataFrame. Think of Series as just a single row of data, (1 X N Array)

# Import pandas and cars.csv


import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Print out country column as Pandas Series


print(cars['cars_per_cap'])

# Print out country column as Pandas DataFrame


print(cars[['cars_per_cap']])

# Print out DataFrame with country and drives_right columns


print(cars[['cars_per_cap', 'country']])

Can also print out certain number of observations like this


Print(cars[0:4]) #prints first 4 observations
Can also select data with the loc and iloc functions, loc is label based while iloc is integer
based
Ex.
# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)
# Print out observation for Japan
print(cars.iloc[2])

# Print out observations for Australia and Egypt


print(cars.loc[['AUS', 'EG']])

Now comes the more advanced, harder stuff to learn


Generators
Use yield to output a value from a for/while loop each time the loop is executed. Yield is
the only way to create true generators. Other “generators” are just nested functions.

List Comprehensions
Can be used to pick out certain values/strings in one line
Ex.
numbers = [34.6, -203.4, 44.9, 68.3, -12.2, 44.6, 12.7]
newlist = [round(number) for number in numbers if number > 0 ]
print(newlist)
EX. 2
sentence = "the quick brown fox jumps over the lazy dog"
words = sentence.split()
word_lengths = [len(word) for word in words if word != "the"]
print(words)
print(word_lengths)

Multiple Function Arguments

Like C and MATLAB, functions can have a specified number of inputs, which can help
with debugging and understanding of your code.

Def myfunction(variable1, variable2, variable3)


Print(“My first Variable is %s” % variable1)
Print(“My second variable is %s” % variable2)
Print(“My third variable is %s” % variable3)
We can also specify a function to have a variable amount of inputs to a function (Like
varargin in MATLAB). This is accomplished with *myothervariables, the myothervariables object
can have whatever amount of variables inside of it.

Def myfunction(variable1,variable2,variable3, *myothervariables)


Print(“Here is all my variables %s %s %s … %s” % (variable1, variable2, variable3,
list(myothervariables))
Additionally, we can choose to have functions output specific things, even if the
arguments are out of order, and the arguments are usually keywords. This is done by **outputs
(outputs can be anything, just has to be preceded by **).

def bar(first, second, third, **options):


if options.get("action") == "sum":
print("The sum is: %d" %(first + second + third))

if options.get("number") == "first":
return first

result = bar(1, 2, 3, action = "sum", number = "first")


print("Result: %d" %(result))

Exception Handling (Debugging)


A form of Python debugging is done using the try/except block, this is done when you
want to do something special to a portion of code which produces an error. If you want to
output an indicator to a portion of code which stops the entire code, use this. In my opinion,
this isn’t very helpful for beginners, but worth knowing.
Example:
def do_stuff_with_number(n):
print(n)

def catch_this():
the_list = (1, 2, 3, 4, 5)

for i in range(20):
try:
do_stuff_with_number(the_list[i])
except IndexError: # Raised when accessing a non-existing index of a list
do_stuff_with_number(0)

catch_this()

Another function to use in exceptions is raise


If arg <0: raise (ValueError, “Negative Argument”) #this prints out “Negative argument”
and line number

Sets
Sets are just lists with no duplicate entries
Print(set(“my name is Eric and Eric is my name”.split()))
This will print {‘my’ , ‘is’, ‘name’, ‘Eric’}, note how there is no order

To see if there is a string/value that is common between two sets, use .intersection
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])

print(a.intersection(b))
print(b.intersection(a))
This will print out {‘John’} {‘John’}

To see everything BUT the common entries between two sets, use .symetric_difference
This function compares two sets simultaneously, so will give two of the same outputs
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])

print(a.symmetric_difference(b))
print(b.symmetric_difference(a))
This prints out {‘Jake, ‘Jill’, ‘Eric’} twice

.difference will compare one set to another, and returns everything BUT the common
value/string and without repeating entries.

a = set(["Jake", "John", "Eric"])


b = set(["John", "Jill"])

print(a.difference(b))
print(b.difference(a))
This prints out {‘Jake, ‘Eric} {‘Jill’}

.union combines two sets into one, and common entries between sets are only
listed/encapsulated once
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])

print(a.union(b))
This prints out {‘John’, ‘Jake’, ‘Jill’, ‘Eric’}

Can add to sets with whatever.add()

Serialization
This is where it gets complicated, so there’s no need to learn this if you don’t deal with
networks or servers. But if you’re interested, Python can be used to encode and decode JSON.
JSON stands for JavaScript Object Notation, and basically what this section does is show that
Python can read and manipulate JavaScript. JSON is either in a string or object datastructure,
and different functions must be used for each one.
In order to use this function first import json or simplejson if using Python 2.5

.loads takes a string and turns it back into JSON datastructure


import json
print(json.loads(json_string))

.dumps takes in an object and spits out a JSON object


import json
json_string = json.dumps([1, 2, 3, "a", "b", "c"])
print(json_string)

You can also just serialize data in Python, to Python. As in, instead of Python data in→ JSON
data out, you can do Python data in→ Python data out. To do this, simply import pickles (or
cpickles), and use the same .loads .dumps functions.

Partial Functions
Not really important, could just call function normally. Advantageous if you have a
function with many constants, but one variable. But basically, replaces first input with entire
function when stated like new=partial(func,var1,var2,etc), then after this new(varremaining)
will execute the original function, with varremaining used as the last variable.

from functools import partial

def multiply(x,y):
return x * y

# create a new function that multiplies by 2


dbl = partial(multiply,2)
print(dbl(4))

Code Introspection
These built in functions help determine what everything inside a class, function,
keyword is and does.

help() #explains what the function you call does


dir() #prints everything inside the dictionary
hasattr() #
id()
type()
repr()
callable()
issubclass()
isinstance()
__doc__
__name__

Closures
Closures can be used to prevent global variables, by using either nested functions/loops
or the nonlocal keyword. This is why spacing is so important in Python.
Ex. 1
def print_msg(number):
def printer():
"Here we are using the nonlocal keyword"
nonlocal number
number=3
print(number)
printer()
print(number)

print_msg(9)
Without nonlocal keyword, we would get “3 9”, but with it, we get “3 3”.
Ex. 2
def transmit_to_space(message):
"This is the enclosing function"
def data_transmitter():
"The nested function"
print(message)
return data_transmitter

fun2 = transmit_to_space("Burn the Sun!")


fun2()
This prints out “Burn the Sun”

Decorators
Just a function which takes in another function and spits a new one out. These are a bit
confusing to learn, but just think when you see @somefunction before another function (let’s
call it newfunc), newfunc replaces the original function defined above in the code. So if you
declare “somefunction(oldfunction)” earlier in the code, newfunc replaces “oldfunction”.
Decorators are basically a way to change nested functions without

@decorator
def functions(arg):
return "value"
Is equivalent to
def function(arg):
return "value"
function = decorator(function) # this passes the function to the decorator, and reassigns
it to the functions
A better example is this:
def repeater(old_function):
def new_function(*args, **kwds): # See
learnpython.org/en/Multiple%20Function%20Arguments for how *args and **kwds
works
old_function(*args, **kwds) # we run the old function
old_function(*args, **kwds) # we do it twice
return new_function # we have to return the new_function, or it wouldn't reassign it
to the value

>>> @repeater Calls the repeater function, and equates “old_function” to “multiply”
function
def multiply(num1, num2):
print(num1 * num2)

>>> multiply(2, 3) Since “multiply” function was set to “old_function”, 6 is printed twice
6
6

We could have decorators to change the output of a function, notice the spacing here
def double_out(old_function): #same as previous example, but repeater changed to
double_out
def new_function(*args, **kwds):
return 2 * old_function(*args, **kwds) # modify the return value
return new_function

Could also use decorator to change input into function


def double_Ii(old_function):
def new_function(arg): # only works if the old function has one argument
return old_function(arg * 2) # modify the argument passed
return new_function

Let’s say you want to multiply the output by a variable amount, the following code could
be used to achieve this utilizing decorators

def multiply(multiplier):
def multiply_generator(old_function):
def new_function(*args, **kwds):
return multiplier * old_function(*args, **kwds)
return new_function
return multiply_generator # it returns the new generator

# Usage
@multiply(3) # multiply is not a generator, but multiply(3) is
def return_num(num):
return num

# Now return_num is decorated and reassigned into itself


return_num(5) # should return 15

How I understood this example is by looking at the top half of code and bottom half of
code as mirrored “replacements” of functions. When @multiply(3) is called, our
old_function is now set to 3. Below @multiply3 decorator is the return_num function,
so return_num is now equated to “multiplier” value (in this case 5). It’s a bit hard to
explain, so I tried color coding above to help understand.

Ex.
def type_check(correct_type):
def check(old_function):
def funccheck(arg):
if (isinstance(arg, correct_type)):
return old_function(arg)
else:
print("Bad Type")
return funccheck
return check

@type_check(int) #correct_type is now int, this line also sets old_function=num*2


def times2(num):
return num*2

print(times2(2)) #executes entire above function block with arg=2


times2('Not A Number') #executes entire function block again, but with arg=’Not A
number’

@type_check(str) #changes “int” to “str” in above function block


def first_letter(word): #changes old_function to word[0]
return word[0]

print(first_letter('Hello World')) #executes above function block with arg=’Hello World’


first_letter(['Not', 'A', 'String']) #executes entire funciton block above with arg=[‘Not’, ‘A,
‘String]
This example prints out
4
Bad Type
H
Bad Type
Map, Filter, Reduce
Three functions that lets one apply a function over multiple iterables. Map, filter,
are built in with Python, so don’t need to import them. Reduce must be imported from
the functools module.
1. In Python 2, map() returns a list. In Python 3, map() returns a map object, which is a
generator object. To get list, use list(map(func, *iterables))
2. Number of args to func must be the number of iterables listed. I.e. equate the input
arguments to func with *iterables
To call map, use map(func,*iterables)

Ex without map
my_pets = ['alfred', 'tabitha', 'william', 'arla']
uppered_pets = []

for pet in my_pets:


pet_ = pet.upper()
uppered_pets.append(pet_)

print(uppered_pets)

Using map
# Python 3
my_pets = ['alfred', 'tabitha', 'william', 'arla']
uppered_pets = list(map(str.upper, my_pets))
print(uppered_pets)

Str.upper() does not have to be used, map() takes care of this for us. Since str.upper()
needs only one iterable, only my_pets must be called after str.upper.

If we have a function with more than one iterable, say the round() function, we must
add both iterables which follow round(). In this example, we want to round the areas of
circles to one decimal point, then two, then three, etc.

# Python 3
circle_areas = [3.56773, 5.57668, 4.00914, 56.24241, 9.01344, 32.00013]
result = list(map(round, circle_areas, range(1,7)))
print(result)
This prints: [3.6, 5.58, 4.009, 56.2424, 9.01344, 32.00013]

If you use a number of iterables which is less than or more than the desired amount,
Python will stop and return the mapping if number of iterables is too little. If the
number of iterables is too much, Python will run the mapping until it reaches the last
available iteration. Using above example, if range(1,3) is used, Python will print out [3.6,
5.57]. If range(1,300) is used, Python will print same thing [3.6, 5.58, 4.009, 56.2424,
9.01344, 32.00013]. Basically, Python will do what it can with given iterable.

Zip() creates a “tuple” from lists, containing pairs of consecutive elements in a new list.

Ex.
# Python 3
my_strings = ['a', 'b', 'c', 'd', 'e']
my_numbers = [1,2,3,4,5]
results = list(zip(my_strings, my_numbers))
print(results)
This prints: [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]

The lambda function is a function that can be created on the spot with no name
associated with it.

# Python 3
my_strings = ['a', 'b', 'c', 'd', 'e']
my_numbers = [1,2,3,4,5]
results = list(map(lambda x, y: (x, y), my_strings, my_numbers))
print(results)

Filter is like the map function, but instead passes iterable objects through Boolean
conditions, and throws away all that are false. It may only be used for functions with
one argument.

Filter(func,iterable)
Ex. 1

# Python 3
scores = [66, 90, 68, 59, 76, 60, 88, 74, 81, 65]
def is_A_student(score):
return score > 75
over_75 = list(filter(is_A_student, scores))
print(over_75)

Ex 2 Palindrome is a word that is same spelled forwards and backwards


# Python 3
dromes = ("demigod", "rewire", "madam", "freer", "anutforajaroftuna", "kiosk")
palindromes = list(filter(lambda word: word == word[::-1], dromes))
print(palindromes)
Reduce applies a function of two arguments cumulatively to the elements in an iterable,
optionally starting with an initial object. Two arguments in this case can be a list of two
or more elements, not two separate objects. Must introduce with from functools import
reduce.

Reduce(func, iterable[,initial])

If Initial is not provided, Python uses the first element of iterable as initial, then the
second element in iterable becomes the first element in iterable.

Ex.
# Python 3
from functools import reduce
numbers = [3, 4, 6, 9, 34, 12]
def custom_sum(first, second):
return first + second
result = reduce(custom_sum, numbers)
print(result)
This prints 68

This adds the first two elements, returns the sum to reduce, then reduce adds that sum
and the third element, sends it back to reduce, etc. If I were to add an initial term, say
10, then this would print out 78.

Here are some sample codes I’ve written:


import random

def create_list(): # creates a list with 50 random integer entries between 1,100
a=[]
for x in range(1,50):
b=random.randint(1,100)
a.append(b)
return a

output1=create_list() #calls the function, and assigns the list a to output1


print(output1)

def new_list(): #makes the list “a” a set, so no duplicate entries


aset=set(output1)
return aset

print(new_list())

Random Password generator


import random

def password_gen():
symbols=["!", "?", "&", ",", "."]
letters_lower=["a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l"]
string_lowers=str(letters_lower)
letters_upper=string_lowers.upper()
letters_upper=list(letters_upper)
numbers=list(str(range(0,10)))

total=symbols + letters_lower + letters_upper + numbers

password=random.sample(total,10)
return password

output1=password_gen()
output3=''.join(output1)
print(output3)

Ex. Cows and Bulls Game


Computer generates random 4 digit number. If you guess a correct digit, in the correct
place, that’s a “cow”. If you guess a correct digit, in the wrong place, that’s a “bull”. This
program also keeps track of number of guesses.

import random

def compare_numbers(winningnum, guess):


cowbull=[0,0]
for i in range(len(winningnum)):
if winningnum[i]==guess[i]:
cowbull[1] += 1
elif winningnum[i] in list(guess):
cowbull[0]+= 1
return cowbull

playing=True
winningnum=str(random.randint(1000,9999))
guesses=0

while playing:
guess=input("Give me your guess")
if guess=="exit":
break
cowbullcount=compare_numbers(winningnum,guess)
guesses += 1

print("You have %s cows and %s bulls" % (str(cowbullcount[1]), str(cowbullcount[0])))

if cowbullcount[1]==4:
print("You have won, with %s cows and %s bulls after %d tries"\
% (str(cowbullcount[1]), str(cowbullcount[0]), guesses))

You might also like