Text-Based Programming: Python PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an introduction to Python programming, including its basic data types, syntax, and applications. It also explains different data types in Python and shows examples.
Full Transcript
SH1904 Text-Based Programming: Python Python is a popular programming language created by Guido van Rossum. It was released in 1991. Python is used for: Web development (server-side); Software development; Mathematics; and System scripting. What can Python...
SH1904 Text-Based Programming: Python Python is a popular programming language created by Guido van Rossum. It was released in 1991. Python is used for: Web development (server-side); Software development; Mathematics; and System scripting. What can Python do? It can be used on a server to create web applications. It can be used alongside software to create workflows. It can connect to database systems. It can also read and modify files. It can be used to handle big data and perform complex mathematics. It can be used for rapid prototyping, or for production-ready software development. Why Python? It works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.). It has a simple syntax similar to the English language. It has syntax that allows developers to write programs with fewer lines than some other programming languages. It runs on an interpreter system, which means that the code can be executed as soon as it is written. Prototyping can be very quick with this. It can be treated in a procedural way, an object-orientated way, or a functional way. Python Syntax compared to other programming languages It was designed for readability and has some similarities to the English language with influence from mathematics. It uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. It relies on indentation, using whitespace, to define scope; such as the scope of loops, functions, and classes. Other programming languages often use curly-brackets for this purpose. Python is an interpreted programming language, which means that as a developer, you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed. The way to run a Python file is like this on the command line: (Where "helloworld.py" is the name of your python file) C:\Users\Your Name>python helloworld.py 08 Handout 3 *Property of STI [email protected] Page 1 of 3 SH1904 Python can also be executed directly in the command line without using a.py file, such as the example below: >>> print("Hello, World!") Hello, World! Data Types Each program language has its own set of data types. These variables are used to store different data and can perform various actions when executed. The following are the built-in data types of Python. Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool Binary Types: bytes, bytearray, memoryview To assign a variable a specific data set, the following commands can be used. x = str("Hello World") str x = int(20) int x = float(20.5) float x = complex(1j) complex x = list(("apple", "banana", "cherry")) list x = tuple(("apple", "banana", "cherry")) tuple x = range(6) range x = dict(name="John", age=36) dict x = set(("apple", "banana", "cherry")) set x = frozenset(("apple", "banana", "cherry")) frozenset x = bool(5) bool x = bytes(5) bytes x = bytearray(5) bytearray x = memoryview(bytes(5)) memoryview Indentations Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important. Python uses indentation to indicate a block of code. if 5 > 2: print("Five is greater than two!") Assigning Variables Python has no command for declaring a variable. Variables are created the moment you assign a value to it. x = 5 y = "Hello, World!" 08 Handout 3 *Property of STI [email protected] Page 2 of 3 SH1904 Comments Python has a commenting capability for in-code documentation. Comments start with a #, and Python will render the rest of the line as a comment. Comments can be used to explain a Python code, make the code more readable, and prevent execution when testing code. This can be placed before or after the code to be executed. Comments can also be more than one (1) line. When writing comments of more than one (1) line, multiline strings can be used (triple quotes). Example 1 #This is a comment. print("Hello, World!") Example 2 #This is a comment #written in #more than just one line print("Hello, World!") Example 3 """ This is a comment written in more than just one line """ print("Hello, World!") Strings and Multiline Strings Strings in Python are identified by either single quotation marks (‘) or double quotation marks (“). Additionally, multiple lines of strings can be assigned to a variable by using either three (3) single quotes (‘ ‘ ‘) or three (3) double quotes (“ “ “) Example 1 print("Hello") print('Hello') Example 2 a = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.''' print(a) REFERENCE W3Schools. (n.d.). Python tutorial. Retrieved from https://www.w3schools.com/python/default.asp 08 Handout 3 *Property of STI [email protected] Page 3 of 3