Introduction
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
- 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.
Syntax
Python syntax can be executed by writing directly in the Command Line:
>>> print("Hello, World!") 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 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!")
Python Variables
In Python, variables are created when you assign a value to it:
Example
x = 5 y = "Hello, World!"
Comments
Python has commenting capability for the purpose of in-code documentation. Comments start with a #, and Python will render the rest of the line as a comment:
Example
#This is a comment. print("Hello, World!")
Variables
Variables are containers for storing data values. Unlike other programming languages, Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.
Example
x = 5 y = "John" print(x) print(y)
Variables do not need to be declared with any particular type and can even change type after they have been set.
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Example
- 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)
Data Types
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
In Python, the data type is set when you assign a value to a variable.
Example | Data Type |
---|---|
x = "Hello World" |
str |
x = 20 |
int |
x = 20.5 |
float |
x = True |
bool |
If...Else Statement
Python supports the usual logical conditions from mathematics. Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
An "if statement" is written by using the if
keyword. The elif
keyword is pythons way
of saying "if the previous conditions were not true, then try this condition". The else
keyword
catches anything which isn't caught by the preceding conditions.
Example
a = 200 b = 33 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print("a is greater than b")
If you have only one statement to execute, one for if
, and one for else
, you can put
it all on the same line. This technique is known as Ternary Operators, or Conditional Expressions.
Example
a = 2 b = 330 print("A") if a > b else print("B")
While Loops
With the while
loop we can execute a set of statements as long as a condition is true. It's
important to increment i, or else the loop will continue forever.
The while
loop requires relevant variables to be ready, in this example we need to define an
indexing variable, i
, which we set to 1.
Example
Print i as long as i is less than 6:
i = 1 while i < 6: print(i) i += 1
For Loops
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.
Example
Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)
Functions
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.To call a function, use the function name
followed by parenthesis.
Example
def my_function(): print("Hello from a function") my_function()
Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:
Example
def my_function(fname): print(fname + " Refsnes") my_function("Emil") my_function("Tobias") my_function("Linus")
Collection Data Types
There are four collection data types in the Python programming language:
- List is a collection which is ordered and changeable. Allows duplicate members.
thislist = ["apple", "banana", "cherry"] print(thislist)
- Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
thistuple = ("apple", "banana", "cherry") print(thistuple)
- Set is a collection which is unordered and unindexed. No duplicate members.
thisset = {"apple", "banana", "cherry"} print(thisset)
- Dictionary is a collection which is unordered, changeable and indexed. No duplicate
members.
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict)
Classes and Objects
Some additional information here Python is an object oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects.
Create a class named MyClass, with a property named x:
class MyClass: x = 5
Create an object named p1, and print the value of x:
p1 = MyClass() print(p1.x)
Reference
All the documentation in this page is taken from w3schools.
You can read more about Python on the Python.org.