You are on page 1of 53

GitHub, Functions, Booleans

and Modules
Prepared By Group 11:
Wyndel S. Albos
Apple Kimberly Calvario
Rieza Jane Naisa
Ma. Angelica Norial
Objectives:
By the end of this presentation students will be able to:

● Identify the difference of Git and GitHub and its components.

● Discuss the continuation on Function, Booleans and other topics in


Programming 1.

● Explain Modules and code structures in Python.


“A programming language is for thinking
about programs, not for expressing
programs you've already thought of. It
should be a pencil, not a pen.”
—Paul Graham
01 Git and GitHub
1st Topic
What is GitHub?
● GitHub facilitates social coding by providing a web
interface to the Git code repository and management
tools for collaboration. GitHub can be thought of as a
serious social networking site for software developers.
Members can follow each other, rate each other's work,
receive updates for specific projects and communicate
publicly or privately.

● At a high level, GitHub is a website and cloud-based


service that helps developers store and manage their
code, as well as track and control changes to their code.
To understand exactly what GitHub is, you need to
know two connected principles:
- Version Control
- Git
What Is Control Version?
● Version control helps developers track and manage changes to a software project’s code. As a
software project grows, version control becomes essential.

● Instead, version control lets developers safely work through branching and merging.

● With branching, a developer duplicates part of the source code (called the repository). The
developer can then safely make changes to that part of the code without affecting the rest of the
project.

● Then, once the developer gets his or her part of the code working properly, he or she can merge that
code back into the main source code to make it official.

● All these changes are then tracked and can be reverted if need be.
What is Git?
What is it used for? And What is Git in GitHub?
The “Git” in GitHub
• Git is a specific open-source version control system created by Linus Torvalds in
2005.

• Specifically, Git is a distributed version control system, which means that the entire
codebase and history is available on every developer’s computer, which allows for
easy branching and merging.

• So, Git is a version control system, but what does that mean? When developers create
something (an app, for example), they make constant changes to the code, releasing
new versions up to and after the first official (non-beta) release.
Demonstration
I will show you the Demonstration of Git and
GitHub and how it will work.
02
Functions
2nd Topic
Functions
What is Function? Defining/Creating Calling Function
 A function is a block of a function  Use the function name followed by
code which only runs
parenthesis.
when it is called. • “def” keyword is use.

Example:
Variable Scope
● Variables in python have a scope which determines where it can be
accessed.

Example:
Local Vs Global
LOCAL GLOBAL
Defined inside a function. Defined outside a function.

Can only be accessed inside the function Can be accessed throughout the program
in which they are declared. and inside every function.

Example:
Global Gotcha
Examples:
Parameters
● Variable listed inside the parentheses in the function definition.

Arguments – value sent or given to the function when it is called.

Example:
Parameters
● Positional - arguments for all positional parameters must be provided
in the order they are listed
● Optional - arguments can be provided positionally and are not
required

Example:
Documentation
● It’s often helpful to leave information in your
code about what you were thinking when you
wrote it.
● This can help reduce the number of WTF(
“Works that Frustrate”)’s per minute in reading
it later.
There are two approaches to this:
Comments Docstrings
Go inline in the body of Strings literals that appear right
code to explain reasoning after the definition of a function
A Docstring should:
● Be a complete sentence in the form of a command describing
what the function does.
● Have a useful single line.
o If more description is needed, make the first line a complete
sentence and add more lines below for enhancement.
● Be enclosed with triple-quotes.

o This allows for easy expansion if required at a later date


o Always close on the same line if the docstring is only one line.
Difference Between Comments and Docstrings
 Docstrings – explain the overall use of a function, methods etc.
 Comments – reminder of what you did and what you need to do.

Recursion
 A process in which a function calls itself.
Example:
03
Boolean
3rd Topic
What is Boolean?
• Python Boolean type is one of the built-in data
types provided by Python, which represents one
of the two values i.e. True or False. Generally, it
is used to represent the truth values of the
expressions.
Boolean Values
• In programming you often need to know if an
expression is True or False.

You can evaluate any expression in Python, and


get one of two answers, True or False.

When you compare two values, the expression is


evaluated and Python returns the Boolean
answer:
Python Boolean Type
The Boolean value can be of two types only i.e. either
True or False.
What is False?
 None
 False

What is True?
 Everything Else

Nothing:  The output <class ‘bool’> indicates


 Zero of any numeric type: 0, 0L, 0.0, 0j. the variable is a Boolean data type.
 Any empty sequence, for example, “”,(),[].
 Any empty mapping, for example, {}.
 Instance of user-defined classes, if the class
defines a __nonzero__() or __len__()
method, when that method returns the
integer zero or bool value False.
Evaluate Variables and
Expression
• We can evaluate values and variables using the
Python bool() function. This method is used to return
or convert a value to a Boolean value i.e., True or
False, using the standard truth testing procedure.

Syntax:
bool([x]) Example: Python bool() method
Example: Python
bool() method
Integer and Floats as Booleans
Numbers can be used as
bool values by using Python’s
built-in bool() method.
Boolean Operators
• Boolean Operations are simple arithmetic of True and False
values. These values can be manipulated by the use of
Boolean operators which include AND, Or, and NOT.
Common Boolean operations are –
• Or
• And
• Not
• == (equivalent)
• != (not equivalent)

• In the above example, we have used Python Boolean with if statement and
OR operator that check if a is greater than b or b is smaller than c and it
returns True if any of the condition is True (b<c in the above example).
Boolean Operators
• The Boolean AND operator returns False if any one of the
inputs is False else returns True.

Boolean and Operators


• The Boolean AND operator returns False if any one of the
inputs is False else returns True.
Boolean == (equivalent) and
!= (not equivalent) Operator
Example: Python Boolean == (equivalent) and != (not equivalent) Operator
IN Operator
• IN operator checks for the membership i.e. checks if the value is present in a
list, tuple, range, string, etc.

• Example: in Operator
04
Modules
4th Topic
Contents
1. Code structure
2. Modules and Packages
3. Importing a module
4. Advantages of Modules
5. Listing all the contents of the module
6. Built-in modules
Code Structure You can put a one-liner after the colon:

In Python, the structure of your code is


determined by whitespace.

How you indent your code determines


how it is structured
Block statement:
some code body
some more code body
another block statement
code body in
that block
White spaces is important
in Python.
An indent could be:
 Any number of spaces
 A tab
 A mix of tabs and spaces:
However, it is recommended to use
4 spaces for indentation.

Other than indenting – space doesn’t matter, technically.


Modules and Packages
Python is all about namespaces – the “dots”

name.another_name

The “dot” indicates that you are looking for a name in


the namespace of the given object. It could be:

 name in a module

 module in a package

 attribute of an object

 method of an object
Module
- Is simply a namespace.
- It might be a single file, or it could be a collection
of files that define a shared API.
- Files you write that end in .py as modules.
- A module allows you to logically organize your
Python code. Grouping related code into a
module makes the code easier to understand and
use.
- A module is a Python object with arbitrarily named
attributes that you can bind and reference.
- Simply, a module is a file consisting of Python
code.
- A module can define functions, classes and
variables. A module can also include runnable
code.
How to create a module?
To create module, simply save your codes in a file with
the file extension py.
Namespace
Importing
• The process by which Python code in one
module is made available to Python code in
another module.
• Python's "import" loads a Python module into its
own namespace, so that you have to add the
module name followed by a dot in front of
references to any names from the imported
module that you refer to:
-To access the codes from the modules -To create an alias for the module:

Import Import Modulename as


Modulename new_name
"from" loads a Python module into the
current namespace, so that you can
refer to it without the need to mention
the module name again:
- Accessing specific functions inside
the module:

from modulename import this, that from modulename import this as that
Advantages of Modules
 Reusability
Working with modules makes the code reusable.

 Simplicity
The module focuses on a small proportion of the
problem, rather than focusing on the entire problem.

 Scoping
A separate namespace is defined by a module that
helps to avoid collisions between identifiers.
Package
- Is a module with other modules in it.
- On a filesystem, this is represented as a directory
that contains one or more .py files, one of
which must be called __init__.py.
Example of a Python package:
What is the purpose of
__init__.py?
- It helps the python to determine whether
the directory is a python package.

- Starting from version 3.3 up to the latest, it


is not required to have a __init__.py.
import packagename.modulename

from packagename.modulename import this, that

from package import modulename


from modulename import *
It is not usually recommended because it pulls out everything inside
the module and stores it in the global space.

Listing all the contents of the module


To know the functions/variables/classes inside the module:
Use dir()
dir()
- Without arguments, it returns the list of names in the current local scope.
- With an argument, attempt to return a list of valid attributes for that
object
- For Modules/Library objects, it tries to return a list of names of all the
attributes, contained in that module.
Built-in Modules
- Built-In Modules come with Python
and are installed by default.
- Built-in modules are written in C
and interpreted using the python
interpreter
How to list all the available modules in
your computer?
Type help(‘modules’) in the
console
Thank
CREDITS: This presentation template was
created by Slidesgo, including icons by

You!
Flaticon, infographics & images by Freepik

You might also like