This project aims to introduce the Python programming language,we will go deep and explore python features.
:warning: information/data in
< > should be changed
See also the list of contributors who participated in this project.
- Getting Started
- Install python
- Virtual environment
- Install packages
- Quick start
- Scalar types
- Lists, Tuples, Dictionaries.
- ...
- ...
- ...
- ...
- ...
- ...
- ...
- ...
- License
- Resources
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- machine with operating system best choices (Linux/Ubuntu).
- Python >= 3.*.
- pip3.
⚠️ information/data in<>should be changed⚠️
A step by step series of examples that tell you how to get a development env running
- clone the repo to your local machine using
$ git https://github.com/SalAlba/machine-learning.git
$ cd machine-learning
Linux/Ubuntu open the terminal and print, in case if you don't have any information print it mean your machine don't have any python version and you have to install one 😄.
$ python --version
# output >> Python 3.8.5check where is installed
$ whereis python
# output
# python:
# /usr/bin/python2.7
# /usr/bin/python
# /usr/bin/python3.8-config
# /usr/bin/python3.8
# /usr/bin/python2.7-config
# /usr/lib/python2.7
# /etc/python2.7
# /etc/python3.8
# /usr/local/lib/python2.7
# /usr/local/lib/python3.8
# /usr/include/python3.8MacOs open the terminal and print, in case if you don't have any information print it mean your machine don't have any python version and you have to install one 😄.
$ python --version
# output >> Python 3.8.5Windows open the CMD (command line), in case if you don't have any information print it mean your machine don't have any python version and you have to install one 😄.
> python --version
# output >> Python 3.8.5 $ python -m venv <VIRTUAL_ENVIRONMENT_NAME>
# ex.
$ python -m venv venv
$ python -m venv my_envactivate/deactivate virtual environment on linux&macOS
$ source path/to/my_env/bin/activate
# ex.
$ source venv/bin/activate
$ deactivateactivate/deactivate virtual environment on windows
$ path/to/my_env/Script/activate
# ex.
$ source venv/Script/activate
$ deactivateopen anaconda prompt and do the stuff bellow.
By default, environments are installed into the envs directory in your conda directory.
$ conda create --name <VIRTUAL_ENVIRONMENT_NAME>
# ex.
$ conda create --name my_env_nameTo create an environment with a specific version of Python.
$ conda create -n my_env_name python=3.6Specifying a location for an environment.
$ conda create --prefix ./my_env_nameactivate/deactivate virtual environment on linux&macOS&windows
$ conda activate <VIRTUAL_ENVIRONMENT_NAME>
# ex.
$ conda activate my_env_name
$ conda deactivateA python package is a collection of modules. Modules that are related to each other are mainly put in the same package. When a module from an external package is required in a program, that package can be imported and its modules can be put to use. [2.5.]
Any Python file, whose name is the module’s name property without the .py extension, is a module.
pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes. [2.6.]
To install python package
< > should be changed
$ pip install <PACKAGE_NAME>
[...]
Successfully installed PACKAGE_NAME
# ex. of installing package pandas
$ pip install pandas
$ pip install PACKAGE_NAME # latest version
$ pip install PACKAGE_NAME==1.0.4 # specific version
$ pip install 'PACKAGE_NAME>=1.0.4' # minimum version
$ pip install -r requirements.txt # requirements filesInstall python package from file, This is useful if the target machine does not have a network connection:
$ pip install <PACKAGE_NAME.whl>
[...]
Successfully installed PACKAGE_NAMETODO install python package without network and with multi relation with another packages TODO install python package on Linux user account don't have permission
show list of all packages installed
$ pip list
$ pip list --outdatedShow what files were installed:
$ pip show <PACKAGE_NAME>
$ pip show --files <PACKAGE_NAME>Upgrade a package:
$ pip install --upgrade <PACKAGE_NAME>Uninstall a package:
$ pip uninstall <PACKAGE_NAME> $ conda install <PACKAGE_NAME>
[...]
Successfully installed PACKAGE_NAME
# ex. of installing package pandas
$ conda install pandasInstall python package from file, This is useful if the target machine does not have a network connection:
$ conda install <PACKAGE_NAME.whl>
[...]
Successfully installed PACKAGE_NAMETODO install python package without network and with multi relation with another packages
show list of all packages installed
$ conda listShow what files were installed:
$ conda show <PACKAGE_NAME>Upgrade a package:
$ conda install --upgrade <PACKAGE_NAME>Uninstall a package:
$ conda uninstall <PACKAGE_NAME>In case you have user account on linux machine with limit permissions do this
$ cd ~
$ /opt/anaconda3/bin/conda create -n my_root --clone=/opt/anaconda3
$ ~/.conda/envs/my_root/bin/python
$ ~/.conda/envs/my_root/bin/conda install pandas
$ ~/.conda/envs/my_root/bin/conda update --allScalar types Scalar types, have integers (ex. 1, 5, -12), float numbers (ex. 3.14,
Variables we can use variables to store different (any) values like numbers, strings, objects, we create variables using operator equal =, ex. below show how to create variable called x storing integer value.
x = 2
print(x)
print(x + 3)A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
Strings are sequences of characters, in Python3 case they are full Unicode.
str1 = "Hi There!"
str2 = 'Hi There!'
str3 = '''
Hi there
What to do
'''
str4 = """
Hi there
What to do
"""
print(str1)
print(str2)
print(str3)
print(str4)Lists, Tuples, Dictionaries. objects which can store collections
Lists
list_a = [1,2,3]
print(list_a)
list_b = [1,'a',3, 'd']
print(list_b)Tuples
# ex.1.
tuple_a = 1, 2, 3
print(tuple_a)
# ex.2.
tuple_b = (1, 2, 3)
print(tuple_b)list vs tuple
list are mutable (we can change the values in list).
tuple are immutable (we can't change the values in tuple).
# lists
list_a = [1,2,3]
print(list_a[1]) # 2
list_a[1] = 55 # mutable
print(list_a[1]) # 5
# tuples
tuple_a = 1, 2, 3
print(tuple_a[1]) # 2
# tuple_a[1] = 55 # immutable
# print(tuple_a[1])
# TypeError: 'tuple' object does not support item assignmentDicts
dict_a = {
'key_1': 'value_1',
'key_2': 2,
'key_3': True,
'key_4': [],
'key_5': {},
'key_6': (),
'key_7': object,
'key_8': None,
9: 9,
10: 10,
}
print(dict_a)Sets
set_a = {'ada', 1, 'ada', 9, False, 'b'}
print(set_a)sets save just the unique values, in ex. above will be just one
ada.
x = 2
if x > 1:
print('more than 1')
if x == 1:
print('equal 1')
else:
print('not equal')for i in range(4):
print(i)for i in [4,6,8,9]:
print(i)x = 3
while x > 0:
print(x)
x -= 1def func_1():
print('Hi there')
func_1()def pow_2(arg1):
return arg1**2
r = pow_2(4)
print(r)def sum(arg1, arg2):
return arg1 + arg2
r = sum(3,6)
print(r)def func_2(arg1, arg2, *args, **kwargs):
print(arg1)
print(arg2)
print(args)
print(kwargs)
func_2('ada', 33)
func_2('ada', 33, False, 44)
func_2('ada', 33, show=False, age=44)A scalar is a type that can have a single value such as 42, 3.14, or 'Hi there'.
The commonly used scalar types in Python are:
-
int : Any integer.
-
float : Floating point number (64 bit precision).
-
complex : Numbers with an optional imaginary component.
-
bool : True, False.
-
str : A sequence of characters (can contain unicode characters).
-
bytes : A sequence of unsigned 8-bit entities, used for manipulating binary data.
-
NoneType (None) : Python's null, every instance of None is of NoneType.
Python doesn't have primitive types, every type is an object.
int_ = 123
float_ = 3.14
complex_ = 1 + 2j
str_ = 'Salem'
bool_ = True
none_ = None
print(int_)
print(type(int_)) # <class 'int'>
print('Is object :', isinstance(int_, object)) # True
print()
print(float_)
print(type(float_)) # <class 'float'>
print('Is object :', isinstance(float_, object)) # True
print()
print(complex_)
print(type(complex_)) # <class 'complex'>
print('Is object :', isinstance(complex_, object)) # True
print()
print(str_)
print(type(str_)) # <class 'str'>
print('Is object :', isinstance(str_, object)) # True
print()
print(bool_)
print(type(bool_)) # <class 'bool'>
print('Is object :', isinstance(bool_, object)) # True
print()
print(none_)
print(type(none_)) # <class 'NoneType'>
print('Is object :', isinstance(none_, object)) # True
print()quick view of operations can be done on types.
Assignment
we use for that the equal symbol =.
# name_of_var = some_value
a = 12Arithmetic Operators
'''
+ Addition
- Subtraction
* Multiplication
/ Division
// Floor Division
% Modulo
** Power
'''
print(1 + 2)
print(3.14 + 5)
print(5 - 3)
print(2 - 3.14)
print(6 * 2)
print(2 ** 3)
print(4 ** 2)
print(4/2)
print(8/4)
print(4//2)
print(8//3)
print(4%2)
print(4%3)
print(2%4)
print(1%4)
print(3%4)more about modulo
# https://www.freecodecamp.org/news/the-python-modulo-operator-what-does-the-symbol-mean-in-python-solved/
print('0%3 : ', 0%3)
print('1%3 : ', 1%3)
print('2%3 : ', 2%3)
print('3%3 : ', 3%3)
print('4%3 : ', 4%3)
print('5%3 : ', 5%3)
print('6%3 : ', 6%3)
print('7%3 : ', 7%3)
print('8%3 : ', 8%3)
print()
print('-0%3 : ', -0%3)
print('-1%3 : ', -1%3)
print('-2%3 : ', -2%3)
print('-3%3 : ', -3%3)
print('-4%3 : ', -4%3)
print('-5%3 : ', -5%3)
print('-6%3 : ', -6%3)
print('-7%3 : ', -7%3)
print('-8%3 : ', -8%3)where can we use modulo
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
print(days[ (1) % len(days) ])
print(days[ (7) % len(days) ])
print(days[ (6) % len(days) ])
print(days[ (6+1) % len(days) ]) # Sun + 1 = Mon
print(days[ (6+3) % len(days) ]) # Sun + 3 = Wed
print(days[ (1-1) % len(days) ]) # Tue - 1 = Mon
print(days[ (1-5) % len(days) ]) # Tue - 5 = Fri
print(days[ (1-14) % len(days) ]) # Tue - 14 = Tue
print(days[ (1-15) % len(days) ]) # Tue - 15 = MonComparison Operators
'''
== Equal To
> Greater Than
>= Greater Than or Equal To
< Less Than
<= Less Than or Equal To
!= Not Equal
'''
print(4 == 4)
print(3 == 1)
print(True == False)
print(3.14 == 3.14)
print(1 == 1.0)
print(1 < 1.0)
print(True > False)
print( 1 < 2 < 3)
print( 5 >= 4 == 4)Boolean Operators
and
or
not
-----------------------------------
and
-----------------------------------
a b | q
-----------------------------------
True True | True
True False | False
False True | False
False False | False
-----------------------------------
or
-----------------------------------
a b | q
-----------------------------------
True True | True
True False | True
False True | True
False False | False
-----------------------------------
not
-----------------------------------
a | q
-----------------------------------
True | False
False | True
int_ = 1
float_ = 1.0
complex_ = 1 + 0j
str_ = 'Salem'
bool_ = True
none_ = None
print(int(int_)) # 1
print(float(int_)) # 1.0
print(complex(int_)) # (1+0j)
print(str(int_)) # '1'
print(bool(int_)) # True
print(bool(0)) # False
print()
print(int(float_)) # 1
print(float(float_)) # 1.0
print(complex(float_)) # (1+0j)
print(str(float_)) # 1.0
print(bool(float_)) # True
print(bool(0.0)) # False
print()
# print(int(complex_)) # TypeError: can't convert complex to int
# print(float(complex_)) # TypeError: can't convert complex to float
print(complex(complex_)) # (1+0j)
print(str(complex_)) # (1+0j)
print(bool(complex_)) # True
print(bool((0+0j))) # False
print()
# print(int(str_)) # ValueError: invalid literal for int() with base 10: 'Salem'
# print(float(str_)) # ValueError: could not convert string to float: 'Salem'
# print(complex(str_)) # ValueError: complex() arg is a malformed string
print(int('1')) # 1
print(float('1')) # 1.0
print(complex('1')) # (1+0j)
print(str(str_)) # Salem
print(bool(str_)) # True
print(bool('')) # False
print()
print(int(True)) # 1
print(float(True)) # 1.0
print(complex(True)) # (1+0j)
print(str(True)) # True
print(bool(True)) # True
print()
print(int(False)) # 0
print(float(False)) # 0.0
print(complex(False)) # (0+0j)
print(str(False)) # False
print(bool(False)) # False
print()
# print(int(none_)) # TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
# print(float(none_)) # TypeError: float() argument must be a string or a number, not 'NoneType'
# print(complex(none_)) # TypeError: complex() first argument must be a string or a number, not 'NoneType'
print(str(none_)) # 'None'
print(bool(none_)) # False
print()help(str) help(int)
Replace values
# ...
a = 2
b = 3
tmp = a
a = b
b = tmp
# ...
a = 2
b = 3
b, a = a, bTODO ...
This project is licensed under the MIT License - see the LICENSE.md file for details, Copyright 2020 © Salem Albarudy.