Module 1 - Data Scalability and Analytics
Module 1 - Data Scalability and Analytics
Introduction
#1
Python Introduction
What is Python?
It is used for:
Why Python?
Python Syntax
As we learned in the previous page, Python syntax can be executed by
writing directly in the Command Line. Or by creating a python file on the
server, using the .py file extension, and running it in the Command Line.
Python Indentation
if 5 > 2:
print("Five is greater than two!")
Output:
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:
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Output Error:
Python Variables
x = 5
y = "Hello, World!"
Comments
Comments start with a #, and Python will render the rest of the line as a
comment:
#This is a comment.
print("Hello, World!")
Creating Variables
x = 5
y = "John"
print(x)
print(y)
Output:
Variables do not need to be declared with any particular type and can even change
type after they have been set.
x = "John"
# is the same as
x = 'John'
Variable Names
Output Variables
Output:
You can also use the + character to add a variable to another variable.
Python
Introduction
#6
If you try to combine a string and a number, Python will give you an
error:
x = 5
y = "John"
print(x + y)
Output:
Global Variables
Variables that are created outside of a function (as in all of the examples
above) are known as global variables.
Output:
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
Python
Introduction
#7
variable with the same name will remain as it was, global and with the
original value.
Variables can store data of different types, and different types can do
different things.
You can get the data type of any object by using the type() function:
Python
Introduction
#8
x = 5
print(type(x))
Output:
In Python, the data type is set when you assign a value to a variable:
Python Numbers
• int
• float
• complex
Variables of numeric types are created when you assign a value to them:
x = 1 # int
y = 2.8 # float
z = 1j # complex
Type Conversion
You can convert from one type to another with the int(), float(),
and complex() methods:
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(a)
print(b)
print(c)
Python
Introduction
#10
Output:
Python Casting
Specify a Variable Type
Python Strings
String Literals
However, Python does not have a character data type, a single character
is simply a string with a length of 1.
Python
Introduction
#11
Get the character at position 1 (remember that the first character has the position 0):
a = "Hello, World!"
print(a[1])
Output:
Slicing
Specify the start index and the end index, separated by a colon, to return
a part of the string.
b = "Hello, World!"
print(b[2:5])
Output:
Use negative indexes to start the slice from the end of the string.
String Length
a = "Hello, World!"
print(len(a))
Python
Introduction
#12
Output:
String Methods
Python has a set of built-in methods that you can use on strings.
The strip() method removes any whitespace from the beginning or the
end.
The split() method splits the string into substrings if it finds instances of
the separator.
String Format
age = 36
txt = "My name is John, I am
" + age
print(txt)
Python
Introduction
#13
Output:
But we can combine strings and numbers by using the format() method!
The format() method takes the passed arguments, formats them, and
places them in the string where the placeholders {} are:
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
Escape Character
The escape character allows you to use double quotes when you normally
would not be allowed:
Python Booleans
Booleans represent one of two values: True or False.
Boolean Values
You can evaluate any expression in Python, and get one of two
answers, True or False.
When you compare two values, the expression is evaluated and Python
returns the Boolean answer:
print(10 > 9)
print(10 == 9)
print(10 < 9)
Output:
Python
Introduction
#15
Python Operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Identity operators are used to compare the objects, not if they are equal,
but if they are actually the same object, with the same memory location:
Programming Exercise #1
1. Area of Pentagon. Write a program that prompts the user to enter the
length from the center of a pentagon to a vertex and computes the area of
the pentagon, as shown in the following figure.