0% found this document useful (0 votes)
9 views18 pages

Module 1 - Data Scalability and Analytics

Python is a versatile programming language created by Guido van Rossum in 1991, used for web development, software development, mathematics, and system scripting. It features a simple syntax that emphasizes readability, allowing for rapid prototyping and supports various programming paradigms. The document covers Python's syntax, variable creation, data types, and operators, along with practical examples and exercises.

Uploaded by

zuprocyhannie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
9 views18 pages

Module 1 - Data Scalability and Analytics

Python is a versatile programming language created by Guido van Rossum in 1991, used for web development, software development, mathematics, and system scripting. It features a simple syntax that emphasizes readability, allowing for rapid prototyping and supports various programming paradigms. The document covers Python's syntax, variable creation, data types, and operators, along with practical examples and exercises.

Uploaded by

zuprocyhannie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Python

Introduction
#1

Python Introduction
What is Python?

Python is a popular programming language. It was created by Guido van


Rossum, and released in 1991.

It is used for:

• web development (server-side),


• software development,
• mathematics,
• system scripting.

What can Python do?

• 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.

Why Python?

• 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-orientated
way or a functional way.

Python Syntax compared to other programming languages

• Python was designed for readability, and has some similarities to


the English language with influence from mathematics.
Python
Introduction
#2

• 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.

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

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.

if 5 > 2:
print("Five is greater than two!")

Output:

Python will give you an error if you skip the indentation:

if 5 > 2:
print("Five is greater than two!")

 The number of spaces is up to you as a programmer, but it has to be at least one.


Python
Introduction
#3

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

In Python, variables are created when you assign a value to it:

x = 5
y = "Hello, World!"

Python has no command for declaring a variable.

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:

#This is a comment.
print("Hello, World!")

Creating Variables

Variables are containers for storing data values.


Python
Introduction
#4

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.

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.

String variables can be declared either by using single or double quotes:

x = "John"
# is the same as
x = 'John'

Variable Names

A variable can have a short name (like x and y) or a more descriptive


name (age, carname, total_volume). 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 (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three
different variables)
Python
Introduction
#5

#Legal variable names:


myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

#Illegal variable names:


2myvar = "John"
my-var = "John"
my var = "John"

 Remember that variable names are case-sensitive

Assign Value to Multiple Variables

Python allows you to assign values to multiple variables in one line:

x, y, z = "Orange", "Banana", "Cherry"


print(x)
print(y)
print(z)

Output Variables

The Python print statement is often used to output variables.

To combine both text and a variable, Python uses the + character:


x = "awesome"
print("Python is " + x)

Output:

You can also use the + character to add a variable to another variable.
Python
Introduction
#6

For numbers, the + character works as a mathematical operator.

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.

Global variables can be used by everyone, both inside of functions and


outside.

Create a variable outside of a function, and use it inside the function

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.

The global Keyword

Normally, when you create a variable inside a function, that variable is


local, and can only be used inside that function.

To create a global variable inside a function, you can use


the global keyword.

Python Data Types


Built-in Data Types

In programming, data type is an important concept.

Variables can store data of different types, and different types can do
different things.

Python has the following data types built-in by default, in these


categories:

Getting the Data Type

You can get the data type of any object by using the type() function:
Python
Introduction
#8

Print the data type of the variable x:

x = 5
print(type(x))

Output:

Setting the Data Type

In Python, the data type is set when you assign a value to a variable:

Python Numbers

There are three numeric types in Python:


Python
Introduction
#9

• 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

Int, or integer, is a whole number, positive or negative, without decimals,


of unlimited length.

Float, or "floating point number" is a number, positive or negative,


containing one or more decimals.

Complex numbers are written with a "j" as the imaginary part:

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

#convert from int to float:


a = float(x)

#convert from float to int:


b = int(y)

#convert from int to complex:


c = complex(x)

print(a)
print(b)
print(c)
Python
Introduction
#10

Output:

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.

Casting in python is therefore done using constructor functions:

• int() - constructs an integer number from an integer literal, a float


literal (by rounding down to the previous whole number), 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

Python Strings
String Literals

String literals in python are surrounded by either single quotation marks,


or double quotation marks.

'hello' is the same as "hello".

Strings are Arrays

Like many other popular programming languages, strings in Python are


arrays of bytes representing unicode characters.

However, Python does not have a character data type, a single character
is simply a string with a length of 1.
Python
Introduction
#11

Square brackets can be used to access elements of the string.

Get the character at position 1 (remember that the first character has the position 0):

a = "Hello, World!"
print(a[1])

Output:

Slicing

You can return a range of characters by using the slice syntax.

Specify the start index and the end index, separated by a colon, to return
a part of the string.

Get the characters from position 2 to position 5 (not included):

b = "Hello, World!"
print(b[2:5])

Output:

Use negative indexes to start the slice from the end of the string.

String Length

To get the length of a string, use the len() function.

The len() function returns the length of a string:

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 lower() method returns the string in lower case.

The upper() method returns the string in upper case.

The replace() method replaces a string with another string.

The split() method splits the string into substrings if it finds instances of
the separator.

To check if a certain phrase or character is present in a string, we can use


the keywords in or not in.

To concatenate, or combine, two strings you can use the + operator

Study more about String Methods.

String Format

As we learned in the Python Variables chapter, we cannot combine strings


and numbers like this:

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

To insert characters that are illegal in a string, use an escape character.

An escape character is a backslash \ followed by the character you want


to insert.

An example of an illegal character is a double quote inside a string that is


surrounded by double quotes.

The escape character allows you to use double quotes when you normally
would not be allowed:

txt = "We are the so-called \"Vikings\" from the north."


Python
Introduction
#14

Other escape characters used in Python:

Python Booleans
Booleans represent one of two values: True or False.

Boolean Values

In programming you often need to know if an expression is True or False.

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

Operators are used to perform operations on variables and values.

Python divides the operators in the following groups:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators

Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common


mathematical operations:

Python Assignment Operators

Assignment operators are used to assign values to variables:


Python
Introduction
#16

Python Comparison Operators

Comparison operators are used to compare two values:

Python Logical Operators

Logical operators are used to combine conditional statements:


Python
Introduction
#17

Python Identity 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:

Python Membership Operators

Membership operators are used to test if a sequence is presented in an


object:

Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:


Python
Introduction
#18

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.

You might also like