Part 1 Introduction
Part 1 Introduction
Python is a popular programming language. It was created by Guido van Rossum, and
released in 1991.
It is used for:
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.
Example
print("Hello, World!")
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!")
Example
Syntax Error:
if 5 > 2:
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.
Example
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
You have to use the same number of spaces in the same block of code, otherwise Python will
give you an error:
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Creating a Comment
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("Hello, World!")
Comments can be placed at the end of a line, and Python will ignore the rest of the line:
Example
A comment does not have to be text that explains the code, it can also be used to prevent
Python from executing code:
Example
#This is a comment
#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:
Example
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
As long as the string is not assigned to a variable, Python will read the code, but then ignore
it, and you have made a multiline comment.
VARAIBLES IN PYTHON
Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
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.
Example
Example
x = "John"
# is the same as
x = 'John'
Case-Sensitive
Variable names are case-sensitive.
Example
a=4
A = "Sally"
#A will not overwrite a
Variable Names
A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume)
Example :-
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
Example
2myvar = "John"
my-var = "John"
my var = "John"
print(x)
print(y)
print(z)
Example:-
x=y=z=1
Print(y)
Python - Output Variables
The Python print() function is often used to output variables.
Example
x = "Python is awesome"
print(x)
Example
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Example
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
Notice the space character after "Python " and "is ", without them the result would be
"Pythonisawesome".
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 = "John"
print(x + y)
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 = "John"
print(x, y)
Variables can store data of different types, and different types can do
different things.
Example
x=5
print(type(x))
Output:-
<class ‘int’>
x = 20 int
x = 20.5 float
x = 1j complex
Python Numbers
int
float
complex
Variables of numeric types are created when you assign a value to them:
Example:-
x=1 # int
y = 2.8 # float
z = 1j # complex
Int
Example
Integers:
x=1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Output:-
<class 'int'>
<class 'int'>
<class 'int'>
Float
Example
Floats:
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z)
Complex
Complex numbers are written with a "j" as the imaginary part
Example
Complex:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Type Conversion
You can convert from one type to another with the int(), float(),
and complex() methods:
x = float(1)
#convert from float to int:
y = int(2.8)
z = complex(x)
print(x)
print(y)
print(z)
print(type(x))
print(type(y))
print(type(z))
Output:-
1.0
2
(1+0j)
<class 'float'>
<class 'int'>
<class 'complex'>
Type Conversion
You can convert from one type to another with the int(), float(), and complex() methods:
Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Note: You cannot convert complex numbers into another number type.
Random Number
Python does not have a random() function to make a random number, but Python has a built-
in module called random that can be used to make random numbers:
Example
Import the random module, and display a random number between 1 and 9:
import random
print(random.randrange(1, 10))
Python Casting
Specify a Variable Type
There may be times when you want to specify a type on to a variable. This can be done with
casting. Python is an object-orientated language, and as such it uses classes to define data
types, including its primitive types.
int() - constructs an integer number from an integer literal, a float literal (by
removing all decimals), or a string literal (providing the string represents a whole
number)
float() - constructs a float number from an integer literal, a float literal or a string
literal (providing the string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals
Example
Integers:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
Example
Floats:
Example
Strings:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'