Python Basics - 01
December 12, 2024
1 AI for Chemical Engineering - Python Basics
For pdf export of the notebook, install pandoc, miktex for windows and !pip install
nbconvert, then restart the command prompt
[ ]: !pip install nbconvert
1.0.1 Print and comment
[4]: print("Hello Class!") #This is print statement
Hello Class!
1.0.2 Variables
Variables are containers for storing data values.
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
Variable names are case-sensitive.
String variables can be declared either by using single or double quotes:
[41]: x = 5
str = 'Hello, World!'
y = 6
print(x+y)
11
[9]: print(str)
Hello, World!
[10]: print("The result is:", x+y)
The result is: 11
1.0.3 Python List
Lists are used to store multiple items in a single variable.
1
Lists are one of 4 built-in data types in Python used to store collections of data, the
other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Lists are created using square brackets:
List items are ordered, changeable, and allow duplicate values.
[43]: thislist = ["apple", "banana", "cherry"]
print(thislist)
['apple', 'banana', 'cherry']
[14]: print(thislist[1])
banana
A list can contain different data types
[16]: list1 = ["abc", 34, True, 40, "male"]
print(list1)
['abc', 34, True, 40, 'male']
Loop List
[17]: for x in thislist:
print(x)
apple
banana
cherry
Change List Items
[18]: thislist[1] = "mango"
[19]: print(thislist)
['apple', 'mango', 'cherry']
1.0.4 Python Tuples
Tuple items are ordered, unchangeable, and allow duplicate values.
[21]: thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
('apple', 'banana', 'cherry', 'apple', 'cherry')
[22]: print(thistuple[3])
apple
2
1.0.5 Python Sets
Set items are unordered, unchangeable, and do not allow duplicate values.
[23]: thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
{'apple', 'cherry', 'banana'}
1.0.6 Python Dictionaries
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered (for recent versions), changeable and do
not allow duplicates.
[25]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
Ford
Update Dictionary
[27]: thisdict.update({"color": "red"})
Loop Dictionaries
[28]: for x in thisdict:
print(thisdict[x])
Ford
Mustang
1964
red
[42]: for x in thisdict.values():
print(x)
Ford
Mustang
1964
red
[31]: print(thisdict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
[34]: for x, y in thisdict.items():
print(x, y)
3
brand Ford
model Mustang
year 1964
color red
1.0.7 Python Conditional Expressions
While Loop
[36]: i = 1
while i<10:
print(i)
i += 1
1
2
3
4
5
6
7
8
9
If Else
[40]: x = 3
y = 2
if x>y:
print("x is greater than y")
else:
print("y is greater than x")
x is greater than y
1.0.8 Loops in Python
[37]: for x in range(2, 6):
print(x)
2
3
4
5
[38]: for x in range(2, 30, 3):
print(x)
2
5
8
11
4
14
17
20
23
26
29
[ ]: