Learn Python11
Learn Python11
What is Python?
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
Example
code output
Hello, World!
print("Hello, World!")
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
code output
if 5 > 2: error
print("Five is greater than two!")
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.
code output
if 5 > 2: Five is greater than
print("Five is greater than two!") two!
if 5 > 2:
Five is greater than
print("Five is greater than two!")
two!
You have to use the same number of spaces in the same block of code,
otherwise Python will give you an error:
code output
if 5 > 2: error
print("Five is greater than two!")
print("Five is greater than two!")
Python Variables
In Python, variables are created when you assign a value to it:
code output
5
x = 5
y = "Hello, World!" Hello, World!
print(x)
print(y)
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:
code output
#This is a comment. Hello, World!
print("Hello, World!")
Comments can be placed at the end of a line, and Python will ignore the
rest of the line:
code output
print("Hello, World!") #This is a comment Hello, World!
A comment does not have to be text that explains the code, it can also be
used to prevent Python from executing code:
Multiline Comments
Python does not really have a syntax for multiline comments.
code output
#This is a comment
Hello, World!
#written in
#more than just one line
print("Hello, World!")
since Python will ignore string literals that are not assigned to a variable,
you can add a multiline string (triple quotes) in your code, and place your
comment inside it:
code output
"""
Hello, World!
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
code output
x = 5
5
y = "John"
print(x) John
print(y)
Variables do not need to be declared with any particular type, and can
even change type after they have been set.
code output
x = 5
John
x = "John"
print(x)
Casting
If you want to specify the data type of a variable, this can be done with
casting.
code output
x = str(3) 3
y = int(3)
z = float(3) 3
print(x) 3.0
print(y)
print(z)
code output
x = 5 <class 'int'>
y = "John"
print(type(x)) <class 'str'>
print(type(y))
Single or Double Quotes?
String variables can be declared either by using single or double quotes:
code output
x = "John" John
print(x)
#double quotes are the same as single John
quotes:
x = 'John'
print(x)
Case-Sensitive
Variable names are case-sensitive.
code output
a = 4 4
A = "Sally"
Sally
print(a)
print(A)
2myvar = "John"
my-var = "John"
my var = "John"
code output
x, y, z = "Orange", "Banana", "Cherry" Orange
print(x)
print(y) Banana
print(z) Cherry
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
code output
X= y= z = "Orange" Orange
print(x)
print(y) Orange
print(z) Orange
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.
code output
fruits = ["apple", "banana", "cherry"]
apple
x, y, z = fruits
print(x) banana
print(y) cherry
print(z)
print(fruits) ['apple', 'banana', 'cherry']
code output
x = "Python"
Pythonisawesome
y = "is"
z = "awesome"
print(x+y+z)
Notice the space character after "Python " and "is ", without them the
result would be "Pythonisawesome".
code output
x = 5
15
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:
code output
x = 5
error
y = “10”
print(x + y)
int
float
complex
code output
x = 1
<class 'int'>
y = 2.8
z = 1j <class 'float'>
<class 'complex'>
print(type(x))
print(type(y))
print(type(z))
Int
code output
x = 1
<class 'int'>
y = 356562225
z = -3255522 <class 'int'>
print(type(x)) <class 'int'>
print(type(y))
print(type(z))
Float
Float, or "floating point number" is a number, positive or negative,
containing one or more decimals.
code output
x = 1.10
<class 'float'>
y = 1.0
z = -35.59 <class 'float'>
print(type(x)) <class 'float'>
print(type(y))
print(type(z))
Complex
Complex numbers are written with a "j" as the imaginary part:
code output
x = 3+5j
<class 'complex'>
y = 5j
z = -5j <class 'complex'>
print(type(x)) <class 'complex'>
print(type(y))
print(type(z))
Type Conversion
You can convert from one type to another with the int(), float(),
and complex() methods:
code output
#convert from int to float:
1.0
x = float(1)
#convert from float to int: 2
y = int(2.8) (1+0j)
#convert from int to complex:
<class 'float'>
z = complex(1)
print(x) <class 'int'>
print(y) <class 'complex'>
print(z)
print(type(x))
print(type(y))
print(type(z))