Python - Module 1

Maria João Mira Paulo
Variables and built-in functions.

Why Python?

1. Python is a 'high-level language' ➡️ readable and easy to learn;

 

2. Python is an interpreted language. No need for a compiler.

 

2. Python is versatile:

       - Web Development,

       - Machine Learning,

       - Numeric Computing;

Popular websites built with Python

🤔

Installation

1. Pick version: Python 2 vs Python 3;

 

2. Python Installation Link: https://www.python.org/downloads/

 

 

 

3. Install IDE: https://code.visualstudio.com/

recommended
which python
python --version
Just a tip 🧐
integrated development
environment

Check installation

MacBook-Air-de-Mj:~ mariajoaomirapaulo$ python3
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Python 3.7.2 (default, Feb 12 2019, 08:15:36) 
>>> print("Hello World")
Hello World
>>> 

Python

So... Let's get started?

Variables

 ( name-value pairs ) 
Makes it possible to assign a value to a variable. 
#Assign integer value to variable
version = 3 

#Reassign variable value
version = 2.7

#names are case sensitive
Version = 2

#Choose carefully the variable name to be easy to understand in the future
temp_1 = 2 #bad choice
Variable names may contain letters, numbers, and underscores, but must always begin using a letter. 
A single variable can take on multiple values over its lifetime without any problem. 

 

A single variable can take on multiple values over its lifetime without any

problem at all

Variables types

The basic types are:
- Int | whole numbers
- String | list of characteres
- Float | decimals
- Boolean | True or False
- Null | the absence of value

Strings

Varible value represents text.
Strings are always surronded by "" or ''. 
#language is a String
language = 'python'

# Correct syntax
sentence1 = "She said, 'Python is easy'" 

# Wrong syntax... Do you understand why?
sentence2 = "She said, "Python is easy"" 

#Using escape character
sentence2_correct = "She said, \"Python is easy\""







Let's try... 💻

If you want to use "", both as the delimiter and in the string, escape the quotation character by prepending it with a backslash (\).

Variable

Naming is very important.
- Don't make them too long
- Don't make them too short
- Make them descriptive
- They cannot have spaces 
- They must begin with letters or undescore '_'
There are non-python specific rules:
There are python specific rules:
- Use "snake case" when your variable is long enough to have more than one word. 
Example: a_varible_name instead of avariablename or aVariableName

Strings


language = 'python ' + '3'








Use the + sign to concatenate strings. 

Indexing

Each character in a string will be assigned an index.
language = 'python'






y

p

t

h

o

n

1

0

2

3

4

5

Can you guess the value of language[3]? 

-5

-6

-4

-3

-2

-1

String Slicing

y

p

t

h

o

n

1

0

2

3

4

5

name = 'python'
position = len(name)

# start at position 0 and go till de end of the string
name[0:6:1] # python

# start at position 6 and go till de end of the string reverse (backwards)
name[position::-1] # nohtyp

Numbers

Python supports both integers and float numbers.
age = 22
weight = 46.5





Here, age is a integer ( variable type int) and weight is a float number, since it contains a decimal point(variable type float). 
Numeric operations: +, -, *, /, ** (exponentiate)  

Built-in Functions

But first ... What is a function? 

- Is a section of reusable code which performs an action;

- will always have:

       (mandatory) name

       (optional) receive arguments

       (optional) return data

 
 
Relax... These concepts will be clarified later 🤯

Built-in Functions

 - print(): receives an argument and displays that value on the screen.

- len(): receives a string and returns the length of that string.

 
 
Can you print the length of the word "python" on the screen? 

Let's try... 💻

Built-in Functions

 - input(): waits for someone to type at the keyboard, waits for input.

 
 
top_language = input("What is your favourite programming language? ")
print("My favourite programming language is " + top_language)

Let's try... 💻

Built-in Functions

 - str(): converts a number to a string.

 - int(): converts a string to an integer number

- float(): converts a string to a float number

 
 
version = 3

print("You better use python " + str(version) + ".")

Let's try... 💻

Built-in Functions

 - type(): returns variable type.

 
 
version = 3

print(type(version))

Let's try... 💻

String Methods

 - upper(): converts a string to upper case.

 - lower(): converts a string to lower case.

 

 
 
version = "python3"

print(version.upper())

String Methods

Exercises

Ask for two number and return the value of the product of those two numbers. 
1.
Take two number and check whether the sum is greater than 5, less than 5 or equal to 5.
2.
Print the sum of the number of occurences of the character "a" and the number of occurences of the character "o" in the sentence: "Python 3 is awesome".
3.

Python - Module 1

By Maria João Mira Paulo

Python - Module 1

  • 534