Introduction to Python
Introduction to Python
to Python
2
Agenda
Why Python?
The Python documentation also has a detailed guide on how to install and setup
Python here: https://docs.python.org/3/using/index.html
https://colab.research.google.com/
6
If you install either Python interpreter or Anaconda on your computer, you may
also want to install an IDE (Integrated development environment) software on
your computer.
IDE makes it easier for you to build, edit, or debug your Python code.
One of the most popular IDE used is Visual Studio Code (VSCode):
https://code.visualstudio.com/download
Python Basics
Assigning variables
Output: 7
9
Variable types
In Python, there are four primitive data type: Integer, Float, Boolean, and
String
x = 3 # integer Output:
y = 4.5 # float
z = “Hello” # string <class 'int'>
w = True # boolean
<class 'float'>
print(type(x))
print(type(y)) <class 'str'>
print(type(z))
print(type(w)) <class 'bool'>
10
a, b, c = 1, 2, 3 # multiple assignments
print(a) # output: 1
print(a+b-c) # output: 0
a, b = “Hello ”, “World”
print(a+b) # output: “Hello World”
x = 3
print(x)
print("hello world")
print("hello world:", x) #comma way
print("hello world: ", x, x, x, x, x, x, x, x, x, "hello again")
Output:
3
hello world
hello world, 3
hello world: 3 3 3 3 3 3 3 3 3 hello again
Another interesting way to print called f-string 12
What it is exactly?
f-strings, also known as formatted Literal String Interpolation, were introduced in Python 3.6
It is a feature in some programming languages that allows you to embed expressions within string
literals, using a special syntax:
name = “Parun”
age = 24
PI = 3.14159
Source: https://www.w3schools.com/python/ref_func_print.asp
14
Booleans
print("True and False: ", True and False) print("True or False: ", True or False)
print("True and True: ", True and True) print("True or True: ", True or True)
print("False and True: ", False and True) print("False or True: ", False or True)
print("False and False: ", False and False) print("False or False: ", False or False)
Output: Output:
a = 10
b = 10
if a > b:
print(“a is greater than b”)
elif a == b:
print(“a and b are equal”)
else:
print(“b is greater than a”)
You can also use and or or logical operators to check multiple conditions simultaneously:
today = “Saturday”
weather = “Sunny”
if (today == “Saturday”) and (weather != “Rainy”):
print(“Let’s go outside!”)
else:
print(“Staying home again today…”)
Solution #1:
x, y, z = 3, 4, 5
Solution #2:
x, y, z = 3, 4, 5
if x >= y:
if x >= z:
largest = x
else:
largest = z
elif y >= z:
largest = y
else:
largest = z
Arithmetic operations
3 + 3 # = 6
3 / 3 # = 1.0
3 // 3 # = 1
You may want to exactly use some libraries to solve some specific problems.
For example, a library called math provides access to extra mathematical functions:
import math
a = math.cos(2 * math.pi)
b = math.sqrt(25)
print(a)
print(b)
Output:
1.0
5.0
Another way:
Another way:
Answer: Because the input() method receives user input as a string, and string can not be added
directly with an integer value.
Type Conversion
num = input("Give me a random number:")
● int(x) converts x to integer print(“I will add this number with 10”)
● float(x) converts x to float # Here we convert num to integer type
● str(x) converts x to string new_num = int(num) + 10
● bool(x) converts x to boolean print(new_num)
Try…
x = True
int_x = int(x)
print(int_x)
Output: ???
24
While Statement
Just like the If… Else… Statement, make sure your code is indented properly.
25
Resources:
https://www.programiz.com/python-programming/first-program
https://wiki.python.org/moin/BeginnersGuide
https://www.geeksforgeeks.org/python-programming-language/
27
Any doubts?
Contact me via Line or
[email protected]