🎁GET 20% OFF Using Code 👉SCHOOL👈 Until End 2024🎁

Comments

Single Line Comments

Hash symbol # will mark all text until a newline as a comment.

Sample Code# This is a single line comment

Multiline Comments

Python does not have a syntax for multiline comments. One way to achieve a multiline comment could be starting each line with a hash symbol.

Method 1 For Multiline Comments

Sample Code# This is a comment
# that goes on
# multiple lines

The other way to achieve a multiline comment is to use a multiline string because Python ignores string literals not assigned to a variable. A multiline string has tripe quotes.

Method 2 For Multiline Comments

Sample Code“””
This is a comment
that goes on
multiple lines
“”””

# Type Some Code

Syntax

Python has a simple syntax and grammar which embraces one way of achieving a task. It does not use curly brackets to delimit blocks, but semicolons after statements are allowed.

Python uses white-space indentation to delimit blocks. An increase in indentation comes after certain statements. A decrease in indentation signifies the end of the current block. The suggested indentation size is 4 spaces.

Hello World Example

Sample Codeprint(‘Hello world’) # Prints Hello world
# Type Some Code

History

Python is a popular general-purpose scripting language that is maintained by the Python Software Foundation and is widely used for machine learning. It is released under the open source Python Software Foundation License and its official website is https://www.python.org.

Guido van Rossum first released Python as version 0.9.0 in 1991. A new version is released annually with support for 2 years and security updates for another 3 years.

Python can run on the command line or on a web server by an interpreter. Web frameworks exist to develop complex applications. Client-side Ajax-based applications can also be created to run Python using JavaScript.

Python as a scripting language, is commonly used in artificial intelligence projects and embedded is software products such as FreeCAD, Blender, Gimp, Inkscape, Scribus including notation programs.

Table : Recommended Python Versions

VersionReleasedSupport EndSecurity End
3.112022-10-242024-04-012027-10-31
3.122023-10-022025-04-022028-10-31

Chapter 1: Getting started with Python

In this chapter, you will learn about the brief history of Python, its syntax, comments and string formatting. You can use your favorite text editor, IDE or use the built-in IDE. The built-in IDE can also be used to compile your code.

If you use your own text editor or IDE, you will need to also install Python in order for your code to be interpreted. The built-in IDE has syntax highlighting using different colors for the keywords, comments, control-flow statements, variables and other elements.

To use the built-in IDE simple type the code in the IDE and click the compile button to display the results in the output area. Otherwise follow the requirements for the text editor and Python.

Requirements:

  1. Optional text editor for programming recommended
  2. Optional Python installed and configured

About

Edward Ojambo has been programming in various languages since 1985. He works as a freelance programmer creating custom databases, web, desktop and mobile applications. Private one-on-one tutorials are offered for computer programming, website development, custom applications, Linux, email servers, web servers and search engine optimization.

All content was created for educational purposes and is not affiliated with official Python. The content was extracted from examples that Edward Ojambo uses during work-related activities.

The content is released under Creative Commons BY-SA 4.0, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.

Use the content present in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to OjamboShop.com.

Learning Python

PROGRAMMING eBook LEARNING PYTHON OjamboShop.com eBook created by
Edward Ojambo

Certificate

CERTIFICATE OF COMPLETIONThis is presented to:Your NameOnline: Learning PHPDate: 2024-06-22Edward OjamboAuthorOjamboShop.com

Basic Use Of Exceptions

Exceptions can be thrown and caught. Surround code in a try block. If an exception is thrown and its current function scope has no catch block, the exception will “bubble up” the call stack to the calling function until it finds a matching catch block. All finally blocks it encounters along the way will be executed.

Sample Code<?php
function divNumbers ($num1, $num2) {
	if ($num2 === 0) {
		throw new Exception("Division by zero invalid");
	}
	return ($num1/$num2);
}

try {
  $num = divNumbers(2, 0);
} catch (Exception $e) {
  echo "Error: " . $e->getMessage();
  $num = 0;
}
echo “\nNumber is {$num}”;
?>
Sample Code<?php
function checkNumbers ($num1, $num2) {
	if ($num1 !== $num2) {
		throw new Exception("Wrong answer!");
	}
	return ($num1);
}

try {
  $num = checkNumbers(2, 0);
} catch (Exception $e) {
  echo "Error: " . $e->getMessage();
  $num = 0;
}
echo “\nNumber is {$num}”;
?>
Sample Code<?php
function inverseNumbers ($num1) {
	if ( !$num1 ) {
		throw new Exception("Cannot divide by zero!");
	}
	return (1/$num1);
}

try {
  $num = inverseNumbers(0);
} catch (Exception $e) {
  echo "Error: " . $e->getMessage();
  $num = 0;
} finally {
	echo “Finally.”; // This will be always be executed
}
echo “\nNumber is {$num}”;
?>
<?php // Type Some Code ?>

Extend

A class can inherit constants, methods and properties of another class by using keyword extends.

Sample Code<?php
class ExtendClass extends SimpleClass
{
	// Redefine the parent method
	function displayVar()
	{
		echo "Extending class\n";
		parent::displayVar();
	}
}

$extended = new ExtendClass();
$extended->displayVar();
?>
<?php // Type Some Code ?>

Class Declaration

An object is created by an executed constructor function of the class. The class defines how objects behave, providing initial values as member variables and implementations of behaviour via member functions. Use public and private modifiers to define if functions can be used outside the object.

Sample Code<?php
class SimpleClass
{
	// property declaration
	public $var = 'a default value';

	// method declaration
	public function displayVar() {
		echo $this->var;
	}
}
$instance = new SimpleClass();
// This can also be done with a variable:
$className = ‘SimpleClass’;
$instance = new $className(); // new SimpleClass()
?>
Sample Code<?php
class Foo
{
	public $bar;

	public function __construct() {
		$this->bar = function() {
			return 42;
		};
	}
}
$obj = new Foo();
echo ($obj->bar)(), PHP_EOL;
?>
<?php // Type Some Code ?>