









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Python is a popular programming language. It was created by Guido van Rossum, and released in
It is used for: web development (server-side), software development, mathematics, system scripting.
Python can be used on a server to create web applications. Python can be used alongside software to create workflows. Python can connect to database systems. It can also read and modify files. Python can be used to handle big data and perform complex mathematics. Python can be used for rapid prototyping, or for production-ready software development.
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). Python has a simple syntax similar to the English language. Python has syntax that allows developers to write programs with fewer lines than some other programming languages. Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick. Python can be treated in a procedural way, an object-oriented way or a functional way.
Many PCs and Macs will have python already installed. To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe): C:\Users\ Your Name >python –version 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!") Hello, World!
Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. Example if 5 > 2: print("Five is greater than two!")
In Python, variables are created when you assign a value to it:
Variables in Python x = 5 y = "Hello, World!"
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.
Comments starts with a #, and Python will ignore them: Example #This is a comment print("Hello, World!")
Python does not really have a syntax for multiline comments.
String variables can be declared either by using single or double quotes: Example x = "John"
x = 'John'
Variable names are case-sensitive. Example This will create two variables: a = 4 A = "Sally" #A will not overwrite a
A variable can have a short name (like x and y) or a more descriptive name (age, carname, 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 underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables) A variable name cannot be any of the Python keywords.
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories: 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 None Type: NoneType
You can get the data type of any object by using the type() function:
Print the data type of the variable x: x = 5 print(type(x))
Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values:
Arithmetic operators are used with numeric values to perform common mathematical operations: Operator Name Example
**= x **= 3 x = x ** 3 &= x &= 3 x = x & 3 |= x |= 3 x = x | 3 ^= x ^= 3 x = x ^ 3 >>= x >>= 3 x = x >> 3 <<= x <<= 3 x = x << 3
Comparison operators are used to compare two values: Operator Name Example == Equal x == y != Not equal x != y > Greater than x > y < Less than x < y
>= Greater than or equal to x >= y <= Less than or equal to x <= y
Logical operators are used to combine conditional statements: Operator Description Example and Returns True if both statements are true x < 5 and x < 10 or Returns True if one of the statements is true x < 5 or x < 4 not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
Membership operators are used to test if a sequence is presented in an object: Operator Description Example in Returns True if a sequence with the specified value is present in the object x in y
| (^) Bitwise OR == != > >= < <= is is not in not in Comparisons, identity, and membership operators not (^) Logical NOT and (^) AND or (^) OR
There are four collection data types in the Python programming language: List is a collection which is ordered and changeable. Allows duplicate members. Tuple is a collection which is ordered and unchangeable. Allows duplicate members. Set is a collection which is unordered, unchangeable, and unindexed. No duplicate members. Dictionary is a collection which is ordered* and changeable. No duplicate members.
Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Lists are created using square brackets:
Create a List: thislist = ["apple", "banana", "cherry"] print(thislist)
List items are ordered, changeable, and allow duplicate values. List items are indexed, the first item has index [0], the second item has index [1] etc.
Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets.
Create a Tuple: thistuple = ("apple", "banana", "cherry") print(thistuple)
Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is unordered , unchangeable* , and unindexed.
Create a Set: thisset = {"apple", "banana", "cherry"} print(thisset)
Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
Create and print a dictionary:
i = 1 while i < 6 : print(i) i += 1
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
Print each fruit in a fruit list: fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)
To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
Using the range() function: for x in range(6): print(x)
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.
In Python a function is defined using the def keyword:
def my_function(): print("Hello from a function")
To call a function, use the function name followed by parenthesis:
def my_function(): print("Hello from a function") my_function() Example
def add_numbers(num1, num2): sum = num1 + num print("Sum: ",sum)
add_numbers(5, 4)
Arrays are used to store multiple values in one single variable:
Create an array containing car names: cars = ["Ford", "Volvo", "BMW"]
You refer to an array element by referring to the index number. Example Get the value of the first array items: x = cars[0]
class Person: def init(self, name, age): self.name = name self.age = age p1 = Person("John", 36) print(p1.name) print(p1.age)
Objects can also contain methods. Methods in objects are functions that belong to the object. Let us create a method in the Person class:
Insert a function that prints a greeting, and execute it on the p1 object: class Person: def init(self, name, age): self.name = name self.age = age def myfunc(self): print("Hello my name is " + self.name) p1 = Person("John", 36 ) p1.myfunc()