Data types
String
"Hello world"
Strings are basically characters surrounded by quotes. It can be single quotes like 'this'
or it could be double quotes, like "this"
\
: is a special character in Python's String
\n
: creates a new line
print("This is line 1 \nThis is line 2")
result:
This is line 1
This is line 2
Getting the length of a string
len()
is short for 'length', it returns the length of a string, list or tuple.
>>> len("Spiderman: No Way Home")
22
>>> #each space is included as a character
Concatenate
Concatenate: to combine 2 or more strings into 1 string.>>> "Hello" + " " + "World"
'Hello World'
name = "Ryan"
name = name + " is smart"
print(name) # 'Ryan is smart'
'''
instead of
name = name + ...
you can do
name += ...
'''
name = "Ryan"
name += " is smart"
print(name) #'Ryan is smart'
String formatting
Let's say we want to create a string like soHis name is Ryan. Ryan likes to code. Ryan is 18 years old.
manually changing the name from
Ryan
to Hanate Wakuso Shiseo Tadashite Teriyaki Suzuki Honda Civic
can be a little annoying, so we use string formatting to change the string faster.
Level 1: using '+'
name = "Ryan"
hobby = "code"
age = 18
res = "His name is " + name + ". " + name + "likes to code."
print(res) #'His name is Ryan. Ryan likes to code.'
res += " " + name + "is " + str(age) + " years old."
#str(age) is necessary because int + str will result in an error
print(res) #His name is Ryan. Ryan likes to code. Ryan is 18 years old.
Level 2: old school %s %i
name = "Ryan"
hobby = "code"
age = 18
res = "His name is %s." % name
print(res) #'His name is Ryan'
res = "His name is %(name)s. %(name)s likes to %(hobby)s. %(name)s is %(age)i years old." % {'name': 'ryan', 'hobby':'code', 'age': 18}
print(res) #His name is Ryan. Ryan likes to code. Ryan is 18 years old.
Level 3: .format()
name = "Ryan"
hobby = "code"
age = 18
res = "His name is {}".format(name)
print(res) #His name is Ryan
res = "His name is {name}. {name} likes to {hobby}. {name} is {age} years old.".format(name=name, hobby=hobby, age=age)
print(res) #His name is Ryan. Ryan likes to code. Ryan is 18 years old.
Level 4: f-strings
name = "Ryan"
hobby = "code"
age = 18
res = f"His name is {name}"
print(res) #His name is Ryan
res = f"His name is {name}. {name} likes to {hobby}. {name} is {age} years old."
print(res) #His name is Ryan. Ryan likes to code. Ryan is 18 years old.
change the line from
name = "Ryan"
to name = "Hanate Wakuso Shiseo Tadashite Teriyaki Suzuki Honda Civic"
String slicing
>>> word = 'password'
>>> word[1:4]
'ass'
string[index 1:index 2]
index 1:
- starting of with the desired index
- remember that index starts with 0 in Python
- for convenience, remember that
index 2 = index 1 + length of substring
Integer and Floats
>>> 1.0 + 2
3.0
>>> 6 * 0.5
3.0
>>> 6 / 2
3.0
>>> 7 % 2 #remainder
1
>>> 9 % 5
4
>>> 7 // 2 #round down to nearest whole number
3
>>> 9 // 2.0
4.0
Exponents/power
>>> 5**2 #5^2 or 5*5
25
>>> 5**3 #5^3 or 5*5*5
125
Pro tip
#instead of
a = a + 1
#we can do
a += 1
#or even
a += 1.5
a += 5
Lists & tuples
ARRAYS ALWAYS START FROM 0 (common rule in popular programming languages)Arrays from other programming languages are equivalent to Python's list.
A list can store multiple values such as strings, int, float, boolean and even other lists.
list1 = ['hello world', True, 9, ['another list lol', 'lmao']]
A tuple (pronounced tyu-pel) is like a list, but the contents of it cannot be changed.
>>> diamond_tools = ('sword', 'pickaxe', 'shovel', 'axe')
>>> diamond_tools.append('hoe')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
>>> list1 = ['zero', 'one', 'three']
>>> list1[0]
'zero'
>>> list1[1]
'one'
>>> list1[2]
'three'
.append()
adds another item into the list.
fruits = ['watermelon', 'banana', 'apple']
print(fruits) #['watermelon', 'banana', 'apple']
fruits.append('pineapple')
print(fruits) #['watermelon', 'banana', 'apple', 'pineapple']
>>> list1 = ['one', 'two', 'three']
>>> del list1[0]
>>> list1
['two', 'three']
>>> list1.pop(0)
'one'
>>> list1
['two', 'three']
>>> list1.remove('one')
>>> list1
['two', 'three']
Option 3 is used when you have the element data
Check if item is in list
>>> 5 in [1, 2, 3, 4, 5]
True
>>> 6 in [4, 3, 2, 1]
False
>>> BTS = ["Suga", "Jungkook", "RM", "Jin", "Jimin", "J-Hope", "V"]
>>> "Ryan" in BTS
False
Extra
In Python, strings can sometimes be treated like a list>>> "1" in "1234567890"
True
>>> name = "Ryan"
>>> name[0]
'R'
>>> name[2]
'a'
Boolean
Expressions
==
is to verify that A is equal to B
!=
is to verify that A is NOT equal to B
"hello" == "hello"
returns True
1 == 5
returns False
Logical operators
>>> True and True
True
>>> True and False
False
>>>
>>> True or False
True
>>> False or False
False
>>>
>>> not False
True
>>> not True
False
>>> 'one' not in ['two', 'three', 'four']
True
Conversion between data types
str()
int()
float()
list()
tuple()
bool()
'5'
to be an integer so I can add 415
to it.
if I do this, I get an error
>>> '5' + 415
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
so I convert
'5'
using int()
by doing
>>> int('5')
5
>>> num = '5'
>>> num2 = int(num)
>>> num2 + 415
420