View on GitHub

Python-Tutorial

https://github.com/kdheepak/Python-Tutorial

Introduction to Python

print("hello world")
1 + 1
2 * 4

This is a markdown cell

This is a code cell

Use m and y to convert between code cells and markdown cells

"This is a code cell"

Expressions

Math

2 + 2
( 2 + 2 )
( 2 + 2 ) * ( 3 + 3 )
2 + 2 * 3 + 3
22 / 7
22 /              7
1 - 1
1 -
1 -

print( 2 ** 10 )
1 + - 1

Strings

"Hello World"
'Hello World'
"Hello'
"I'm at a Python Tutorial"
'I\'m at a Python Tutorial'
"I\'m at a Python Tutorial"
"Hello" + 5
"Hello" * 5
"Hello" + " " + "World"
"na" * 10 + " Batman"
("na" * 10) + " Batman"

Variables

var1 = 1
var2 = 2
var3 = 3
var1 + var2 + var3
var1 = "Python"
var2 = "is"
var3 = "Fun"
var1 + var2 + var3
var4 = var1 + var2 + var3
var4

Exercise

variable1 = 10
variable2 = 20
print(variable1, variable2)
variable2, variable1 = variable1, variable2
print(variable1, variable2)

Python types

2
type(2)
int(2)
int(2.0)
int(2.5)
int("2")
int("2.0")
float(2.0)
float(2)
float("2")
type("2")
type(2.0)
type(2)
type(int)
type(type)
variable = "this is a string"
type(variable)
variable = 2
type(variable)
variable = 2.0
type(variable)
[1, 2, 3, 4, 5]
variable = [1, 2, 3, 4, 5]
type(variable)
1 + 2.0
"1" + 3.0
int("1") + 3.0

Valid variable names

string
"variable1"
string = ""
string
"variable1"
variable1 = "valid"
1variable = "invalid"
#variable = "invalid"
variable_1 = "valid"
variable_______1 = "valid"
______variable______ = "valid"
variable = None
variable
print(variable)

f strings

month = "Aug"
date = "22nd"
"The month is " + month + ", and the date is " + date
f"The month is {month}, and the date is {date}"
"The month is {month}, and the date is {date}"

Exercise

age = 99
"I am " + age + "years old."

Exercise-1

Exercise

1) What does "2" + "3" give you in Python?

2) Interchange the values in variable1 and variable2


variable1 = 10
variable2 = 20

Hint: You can use a separate new variable

variable1 = 10
variable2 = 20
print(variable1, variable2)
20 10

Exercise-2

** Exercise **

Find what is wrong with the following expression. Can you fix it?

age = 99
"I am " + age + " years old."
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-104-552dcf048967> in <module>()
----> 1 "I am " + age + " years old."


TypeError: must be str, not int

Can you make the expression work without changing the expression?

What happens when you use f strings to create the string above?