Exemple #1
0
import useful_tools

print(useful_tools.roll_dice(6))

# https://docs.python.org/3/py-modindex.html
import useful_tools as ut

print(ut.beatles)

print(ut.roll_dice(8))

# Modules - builtin or external

# Installed it through PIP



Exemple #3
0
import useful_tools  #trying to import a different python file/module

print(useful_tools.roll_dice(10))  #calling function out of that file/module

import random

feet_in_mile = 5280
meters_in_kilometer = 1000
beatles = ["John Lennon", "Paul McCartney", "George Harrison", "Ringo Star"]

def get_file_ext(filename): #give it a file name and it will tell you the extension
    return filename[filename.index(.) + 1:]

def roll_dice(num):
    return random.randint(1, num)
#instead of copying and pasting this file we can import all of these functions
'''new file '''
import useful_tools #will go grab all the stuff from the file above

print(useful_tools.roll_dice(10)) #now we can access all of the attributes, when we put the cursor it should pop up
#We want to roll a ten sided dice. #We didn't have to copy anything we just imported
#Allows us to write something once and we can repeatedly use it
#Use google modules function. It's python code and variables that are already written for you
#They allow you to make your program better.
#Very is to find 3rd party modules.
#You can save a lot of time using modules
#Modules are either built-in (automatic access) or external.
#External modules are stored in the same file that we stored Python in.

#Lib = stores external modules. They are stored inside lib folder.
#source code tells you where the module is stored.
#We can install third party modules.
'''installing 3rd party modules'''
'''pythondocx = allows you to format and create google documents in python.
pip install python-docx allows you to install python docx using this program (comes pre-installed with python 3)
Exemple #5
0
random_file.write("\nSome Numbers : " + str(rannum1) + " " + str(rannum2) +
                  " " + str(rannum3) + "")
random_file.write("\nMore Numbers : " + str(rannum1) + " " + str(rannum2) +
                  " " + str(rannum3) + "")
random_file.write("\nAnd some more Numbers : " + str(rannum1) + " " +
                  str(rannum2) + " " + str(rannum3) + "")
random_file.close()

print("#####################################################")
print("#####################################################")
print("Modules and pip")
print(
    "A module is basically another python file you can import into your current project"
)
import useful_tools
print(useful_tools.roll_dice(35))
print(
    "As of today (May 9, 2019), a full list of python modules can be found in https://docs.python.org/3/py-modindex.html"
)
print(
    "Installing modules outside of python's local repository is easy, but requires a little bit of extra effort"
)
print(
    "To install modules, you must use the pip from the terminal, or cmd if using windows"
)
print(
    "The command to execute is pip (it's the same for Linux and Windows), and just run it as:"
)
print(">pip install <module-name-to-install>")
print(
    "as an example, you can look online for external python modules, and run the command"
Exemple #6
0
# Modules and Pip (3:28:13)

## Modules
import useful_tools as ut

result = ut.roll_dice(10)
print(result)


## Pip
# Ruta: c:\>cd C:\Users\kani\AppData\Local\Programs\Python\Python38-32
# Cómo instalar si no lo encuentro: https://github.com/BurntSushi/nfldb/wiki/Python-&-pip-Windows-installation
# C:\Users\kani\AppData\Local\Programs\Python\Python38-32\Scripts>pip --version
# Dónde se guardan los paquetes descargados? C:\Users\kani\AppData\Local\Programs\Python\Python38-32\Lib\site-packages

# Ejemplo de instalar un paquete externo:
## C:\Users\kani\AppData\Local\Programs\Python\Python38-32\Scripts
## pip install python-docx
## Revistar la ruta site-packages

# Ejemplo de como desinstalar un paquete externo
## C:\Users\kani\AppData\Local\Programs\Python\Python38-32\Scripts
## pip uninstall python-docx
Exemple #7
0
import useful_tools

print(useful_tools.roll_dice(10))

x = useful_tools.roll_dice(10)
y = useful_tools.roll_dice(10)

print(x)
print(y)
print(useful_tools.add(x, y))
def roll_dice(num):
    return random.randint(1,
                          num)  # this function will roll a di/dice by the number you give it. Example, pass it a 6 you will get a 6 sided di


# Switching to a new file now "app.py"

# At the top of the python file, line 1 write:

import \
    useful_tools  # this will likely show up gray on the IDE. Look to line 9 for filename, this is example based real life will depend on file name

print(
    useful_tools.)  # Once you type this, your IDE will start to give you suggestions of code from that previous file. Those functions, variables, and etc will pop up

print(useful_tools.roll_dice(
    10))  # And this is how you pull a function from the file you imported. Won't work here but this is the dice rolling function from the other file

# This is really a core concept in python. Importing functionality from other python files

# You can write something once and use it whenever you want

# Google List of Python modules and choose the ones for the version you're using. This will find a lot of code that has already been written for you.

# Look for python module doing "insert what you're trying to do here" There probably is one that someone has done already

# Where are these online python modules, where are they stored?

# 2 types: Built in modules and external modules

# Lib folder under the "External Libraries" file tree in PyCharm hosts all the external modules. Lib is a very important folder
Exemple #9
0
###Modules & Pip
##Modules allow you to utilize code & functions from external files in your current Python code

import useful_tools

print(useful_tools.roll_dice(10)) #place name of function after file name

''' Go to doc.python.org or google "Python Module Index" in Google
 for list of common Python Modules'''

'''https://docs.python.org/3/py-modindex.html'''

'''External Modules located in External Libraries>>>
< Python 3.9 >>> lib folder'''

pip install python-docx
Exemple #10
0
#HW28-1 Modules and pip
#module: a python file that can import into current python file

import useful_tools  #import file:useful_tools

print(useful_tools.roll_dice(10))  #use the function from that file

#check https://docs.python.org/3/py-modindex.html can find more functions
#there are two types of models
#1. built in modules (can automatically have access to them)
#2. external modules (are stored in lib folder)
#pip is the statement which is used to install new new modules inside out code fie
#you can check the version of the pip by going in the terminal at the bottom of the pycharm and typing pip --version
#you can install the module by pip for example by typing pip install python-docx        #it will install docx module inside out python project
import useful_tools

print(useful_tools.roll_dice(10))     #it will roll the dice from one till 10

Exemple #12
0
# Like lists, but with restrictions
# cannot be edited after it has been created (immutable)
# use for data that will not change

coordinates = (4, 5)
coordinates[1] = 2  # will give an error
print(coordinates[0])

#create a list of tuples
coordinates = [(4, 5), (4, 5), (4, 5)]


## Functions
# indentation matters

def percentage_diff(number_1, number_2):
    result = (number_1 - number_2) / number_2
    return result

print(percentage_diff(3, 2))

# Importing functions that you have written from other files
from useful_tools import roll_dice, file_extension
roll_dice(6)
file_extension("master_data.csv")

# 4. Importing



Exemple #13
0
# 27th Excise: Modules and Pip
# YouTube Vid: Learn Python - Full Course for Beginners
# URL: https://youtu.be/rfscVS0vtbw

# Python Module Index list - https://doc.python.org/3/py-modindex.html

# An example to import a module
# Import the useful_tools file that is in the same directory
import useful_tools

# calls the roll_dice function in the useful_tools file
print("You rolled a : " + str(useful_tools.roll_dice(9)))

# Installed python-docx using pip
# Import example
import docx

# Proof I d/l and can use the Python-Docx module
# Calling the version number function
print("Python Doc Version: " + str(docx.__version__))
import useful_tools

num = useful_tools.roll_dice(100)
print(num)

print(useful_tools.feet_in_mile)
Exemple #15
0
# don't need to set up a variable for this,
# don't need to specify path as a string

import useful_tools

print(useful_tools.roll_dice(82))

print(useful_tools.feet_in_mile)

# check python documentation for lots of other
# modules you can import

# importing someone elses module - where are they stored
# some are built into python
# external ones stored on someone elses computer
# (in pycharm) python > external libraries > lib
# documentation would normally just show their source in the top left

# installing 3rd party modules
# use python to create word documents
# 'python-docs'
# run 'pip3 install python-docx' (pip3 package manager - like npm)
# gets saved in external libraries > site-packages
# you can uninstall with pip3 uninstall {name}
Exemple #16
0
import useful_tools

print(useful_tools.roll_dice(20))
Exemple #17
0
#Modules and pip
#using modules in python
#modules are external python files that can be imported to current python file and all the functions and other stuffs from that file can be used in the current file after importing it

import useful_tools  #importing external python file or module
print(useful_tools.roll_dice(10))  #importing function from external file

#find list of all supported modules
#goto google.com
#list of pyython modules
#click the docs website of the version using(3.8)
#huge list of python modules can be found here that can be imported and used according to needs
#third party modules can also be found outside of python docs
#module for a specific purpose can also be found out there in google written by other developers or peoples out there
#lot of useful and other modules may not be in module docs in python website or in lib folder in installation directory by default

#two types of modules
#built-in modules - built in with the interpreter
#external modules - are stored where python is installed in the system
#stored inside lib folder where python is installed
#the location of external modules can be found in modules docs
#click on the desired module to know it's location if present in install directory of python

#installing python modules that are not pre-installed
#find the module to be installed
#ex:- google ->python-docx
#goto python-docx website
#follow the install instruction
#can be installed using pip install python-docx

#pip(comes pre-installed with python 3 or later)