Monthly Archives: September 2021

4.4 Global Variables

Variables that are created outside of a function (as in all of the examples above) are known as global variables. Global variables can be used by everyone, both inside of functions and outside. Example Create a variable outside of a function, and use it inside the function x = “awesome”def myfunc():  print(“Python is ” + x)myfunc() Output:Python is awesome… Read More »

4.3 Output Variables

The Python print statement is often used to output variables. To combine both text and a variable, Python uses the + character: Example x = “awesome”print(“Python is ” + x) Output:Python is awesome You can also use the + character to add a variable to another variable: Example x = “Python is “y = “awesome”z =  x + yprint(z) Output:Python is awesome For numbers, the + character… Read More »

4.2 Assign Multiple Values

Many Values to Multiple Variables Python allows you to assign values to multiple variables in one line: Example x, y, z = “Orange”, “Banana”, “Cherry”print(x)print(y)print(z) Output: OrangeBananaCherry Unpack a Collection If you have a collection of values in a list, tuple etc. Python allows you extract the values into variables. This is called unpacking. Example Unpack a list: fruits… Read More »

4.1 Variable Names

Variable Names A variable can have a short name (like x and y) or a more descriptive name (age, car name, total_volume). Rules for Python variables: 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… Read More »

4. Variables

Variables are containers for storing data values. Creating Variables Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Example x = 5y = “John”print(x)print(y) Output: 5John Variables do not need to be declared with any particular type, and can even change type after they have been… Read More »

3. Comments

Comments can be used to explain Python code. Comments can be used to make the code more readable. Comments can be used to prevent execution when testing code. Creating a Comment Comments starts with a #, and Python will ignore them: Example #This is a commentprint(“Hello, World!”) Comments can be placed at the end of a… Read More »

2. Syntax

Execute Python Syntax As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line: >>> print(“Hello, World!”) Output:Hello, World! Or by creating a python file on the server, using the .py file extension, and running it in the Command Line: C:\Users\Your Name>python myfile.py Python Indentation Indentation refers… Read More »

1. Introduction

What is Python? Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting. What can Python do? Python can be used on a server to create web applications. Python can be used alongside software to create… Read More »

Python

Introduction Python Installation Syntax Comments Variables Variable Names Assign Multiple Values Output Variables Global Variables Data Types Numbers Casting Strings Slicing Strings Modify Strings String Concatenation Format-Strings Escape Characters String Methods Booleans Operators Lists Access List Items Change List Items Add List Items Remove List Items Loop Lists List Comprehension Sort Lists Copy Lists Join… Read More »

59. Error handling during file operations

Sometimes during file operations, errors may also creep in. For example, a file being opened for reading might not exist. Or a file name used for a new file may already exist. Or an attempt could be made to read past the end-of-file. Or such as invalid operation may be performed. There might not be… Read More »