Squoggle
Mac's tech blog
My Python Notes
Posted by on September 1, 2022
Virtual Environments for Python
Python3 is already installed on my Linux distro.
Install Virtual Environments
You will want to use Virtual Environments for Python. Install Virtualenv:
$ pip install virtualenv
Create a Virtual Environment
Create a base directory where you want your Virtual Environments to live:
$ cd ~ $ mkdir -p Python/VirtualEnv
Now create a Virtual Environment within that directory structure. I’m going to use P3VirtEnv to signify it is a Python 3 Virtual Environment.
$ cd ~/Python/VirtualEnv $ virtualenv P3VirtEnv
You should see some message that essentially mean that the Virtual Environment was successfully created. You should see a new directory named ~/Python/VirtualEnv/P3VirtEnv.
Now you need to enter the Virtual Environment and set it active:
$ cd P3VirtEnv/ $ source bin/activate
Your prompt will now show what environment you are logged into. At this point it looks like this:
(P3VirtEnv) mac@Gob:~/Python/VirtualEnv/P3VirtEnv$
This process essentially copies your Python installation form the standard location to this Virtual Environment.
You can test this by seeing where Python is installed
$ which python
/home/mac/Python/VirtualEnv/P3VirtEnv/bin/python
Exit the Virtual Environment by simply typing the word deactivate.
$ deactivate
Your prompt and path are now returned to normal.
sdfsdfsdf
Packages
Python has packages that can be installed.
To see what packages have been installed do:
$ pip list
You should see a list of all the packages that have been installed. Mine looks something like this at this point:
Package Version ---------- ------- pip 22.2.2 setuptools 65.3.0 wheel 0.37.1
To see a list of all packages that have been installed by pip then do:
$ pip freeze
Why the virtualenv package is not listed is a mystery to me at this point.
Now is a good time to check that out. Install Selenium like this:
$ pip install selenium
A whole bunch of stuff gets installed. Now you can check pip freeze again and see all of what got installed
Another library that we will need for making API calls is called Requests. Install it like this:
$ pip install requests
The do the freeze thing to verify.
Install PyCharm
Instructions for downloading and installing PyCharm are located here:
https://www.jetbrains.com/help/pycharm/installation-guide.html
Review Section 2, Chapter 10 in the Class
Variables
When you define a string as a variable you can use a single quote or double quote to encapsulate the string. Numbers do not need to be put in quotes. I think if you put it in quotes then it becomes a string and not a number.
Variables cannot start with a Number, just letters or Underscore. The variable may contain numbers or underscore but cannot start with a number.
Data Types
Integers
Integers are numbers without decimals. They can be positive, negative or zero.
You can do math on integers such as Addition, subtraction, multiplication and division as well as other operations.
If you have multiple operations in a single line you should use parenthesis.
Float
Floats (Floating Point Numbers) are numbers with decimal places.
Operations on floating numbers will result in floating numbers.
In Python if you were comparing 1 vs 1.0 the comparison would be false because the first is an integer and the second is a float.
Strings
Strings are a sequence of single characters and are represented with quotes. Quotes can be single or double.
You can use one type of quote within the other type but not one type within the same type.
For example these is valid:
"Bring me your 'stash' of contraband"
But this is not valid:
"Bring me your "stash" of contraband"
Slicing
Slicing or a Slice is a subset of a String. The index number is the character counting from either right or left of the string and gives you a specific character of the string. See chapter 16 in the course.
If counting left to right you start at 0. If counting right to left you start with -1.
Syntax for slicking is:
variable_name[start index : finish index]
The slice does not include the finish index.
Example:
my_string[0]
Would give the first character of the string.
my_string[0:]
Would give the entire string
my_string[9:12]
Would give characters 9 through eleven. Remember that counting starts at 0 and not 1
Type command
You can determine what data type a variable is with the type command.
Lets assume you have a variable named var. You can get what type of data is with the type command like this:
type(var)
And the class of data will be returned to you.
String Methods
https://docs.python.org/3/library/stdtypes.html#string-methods
Recent Comments