Python
Introduction
Python is a popular programming language.
It was created by Guido Van Rossum, and
released in 1991.
What can Python do?
Python can be used on a server to create web
applications.
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 software development.
Why Python?
Python works on different platforms (Windows, Mac, Linux,
Android 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. Python can be treated in
a procedural way, an object-oriented way or a functional way.
The most recent major version of Python is
Python 3.
Python will be written in a text editor.
It is possible to write Python in an Integrated
Development Environment (IDE), such as Thonny,
Pycharm, Netbeans or Eclipse which are
particularly useful when managing larger
collections of Python files.
Python Syntax compared to other
programming languages
Python was designed for readability, and has some similarities
to the English language .
Python uses new lines to complete a command, as opposed to
other programming languages which often use semicolons or
parentheses.
Python 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.
Install Python
We can download it for free from the following
website: [Link]
Write program in text editor or IDE, save file with
.py extension .
Open command line, navigate to the directory
where saved our file, and run.
The Python Command Line
To test a short amount of code in python sometimes it is quickest and
easiest not to write the code in a file , Python commands can be run as a
command line itself.
Type the following on the Windows, Mac or Linux command line:
C:\Users\Your Name>python
Or, if the "python" command did not work, you can try "py":
C:\Users\Your Name>py
From there you can write any python command.
>>> print("Hello, World!")
we can simply type the following to quit the python command line interface:
>>>exit()
Python Indentation
Indentation refers to the spaces at the beginning of a code
line.
Python uses indentation to indicate a block of code.
Python will give you an error if you skip the indentation.
The number of spaces is up to you as a programmer, the most
common use is four, but it has to be at least one.
You have to use the same number of spaces in the same block
of code, otherwise Python will give you an error.
Ex.
if 5 > 4:
print("Five is greater than Four!")
Python Variables
In Python, variables are created when you assign a value to it.
Variables are containers for storing data values.
Python has no command for declaring a variable.
Ex.
x=5
y = “Kota"
print(x)
print(y)
Casting
If you want to specify the data type of a variable, this can be done with casting.
Example
x = str(3) # x will be '3'
y = int(3.5) # y will be 3
z = float(3.2) # z will be 3.2
Get the Type
You can get the data type of a variable with the type() function.
Example
x=5
y = “Kota"
print(type(x))
print(type(y))
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.
Comments can be placed at the end of a line, and Python will
ignore the rest of the line.
Comments starts with a #, and Python will ignore them.
Multi Line Comments
Python does not really have a syntax for multi line comments.
To add a multiline comment you could insert a # for each line
Or, we can use a multiline string.
Since Python will ignore string literals that are not assigned to
a variable
Example
"""
This is a comment
written in
more than one line
"""
print(“Multi Line Comment")
Variable Names
A variable can have a short name or a more descriptive name.
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 .
Variable names are case-sensitive
String variables can be declared either by using single or double
quotes.
Multi Words Variable Names
Variable names with more than one word can be difficult to read.
There are several techniques you can use to make them more readable:
Camel Case
Each word, except the first, starts with a capital letter:
myVariableName = “Kamal"
Pascal Case
Each word starts with a capital letter:
MyVariableName = “Kamal"
Snake Case
Each word is separated by an underscore character:
my_variable_name = “Kamal"
Many Values to Multiple Variables
Python allows us to assign values to multiple variables in one line:
Example
x, y, z = “A", "B", "C"
print(x)
print(y)
print(z)
Note: Make sure the number of variables matches the number of values, or else you will
get an error.
One Value to Multiple Variables
We can assign the same value to multiple variables in one line:
Example
x = y = z = “A"
print(x)
print(y)
print(z)
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to extract the
values into variables. This is called unpacking.
Example
Unpack a list:
fruits = *“A", “B", “C"+
x, y, z = fruits
print(x)
print(y)
print(z)
Output Function
The Python print() function is often used to output variables and messages.
In the print() function, you output multiple variables, separated by a comma.
Example
x = “A"
y = “B"
z = “C"
print(x, y, z)
You can also use the + operator to output multiple variables:
For numbers, the + character works as a mathematical operator.
Example
x=5
y = 10
print(x + y)
In the print() function, when you try to combine a string and a number with
the + operator, Python will give you an error:
Example
x=5
y = “A"
print(x + y) # give an error
The best way to output multiple variables in the print() function is to separate them
with commas, which even support different data types:
Example
x=5
y = “A"
print(x, y)
Global Variables
Variables that are created outside of a function 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 = “Simple"
def myfunc() :
print("Python is " + x)
myfunc()
If you create a variable with the same name inside a function, this variable will be
local, and can only be used inside the function.
The global variable with the same name will remain as it was, global and with the
original value.
Example
Create a variable inside a function, with the same name as the global variable
x = “Simple"
def myfunc():
x = “Fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
The global Keyword
To create a global variable inside a function, you can use the global keyword.
Example
If you use the global keyword, the variable belongs to the global scope:
def myfunc():
global x
x = “Fantastic"
myfunc()
print("Python is " + x)
Also, use the global keyword if you want to change a global variable inside a function.
Example
x = “Simple"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Python Data Types
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