Basic Python Syntax#

Start-up with VCS#

Create project and use git as version control tool

Built-in Data Structures#

  1. Numeric: integer + float

  2. String

  3. List

  4. Tuple

  5. Set

  6. Dictionary

Numeric#

Integers are whole numbers without a decimal point.

[1]:
# definition
int1 = 6  # regular
int2 = 1E2  # scientific notation
[2]:
# addition
int1 + int2
[2]:
106.0
[3]:
# subtract
int2 - int1
[3]:
94.0
[4]:
# multiplication
int1 * int2
[4]:
600.0
[5]:
# division
int2 / int1
[5]:
16.666666666666668
[6]:
# mod
int2 % int1
[6]:
4.0
[7]:
# floor division (positive)
100 // 6
[7]:
16
[8]:
# floor division (negative)
-100 // 6
[8]:
-17

Floats are numbers that contain a decimal point. (64-bits)

[9]:
dec1 = 9.1
dec2 = 6.89E-2

String#

Strings in Python are sequences of characters enclosed in quotes, either single (') or double (").

[10]:
str1 = "Hello, World!"
print(str1)
Hello, World!
[11]:
# concatenation
str2 = "Hello"
str3 = "World"
result = str2 + " " + str3
print(result)  # Output: Hello World
Hello World
[12]:
# Repetition
str4 = "Hello"
print(str4 * 3)
HelloHelloHello

List#

A list is an ordered collection of items which can be of different types. Lists are mutable, meaning their elements can be changed.

[13]:
# definition
list1 = [i for i in range(10)]
print(list1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[14]:
# access with index
list1[0]
[14]:
0
[15]:
# access the last element
list1[-1]
[15]:
9
[16]:
# modify element
list1[4] = 3.9
list1
[16]:
[0, 1, 2, 3, 3.9, 5, 6, 7, 8, 9]
[17]:
# add a single element to the end of the list
list1.append(5)
[18]:
# add an element at a specific position
list1.insert(1, 15)  # insert 15 to the position with 1 as index
list1
[18]:
[0, 15, 1, 2, 3, 3.9, 5, 6, 7, 8, 9, 5]
[19]:
# remove the first occurrence of a value
list1.remove(2) if 2 in list1 else _
list1
[19]:
[0, 15, 1, 3, 3.9, 5, 6, 7, 8, 9, 5]
[20]:
#  remove an element at a specific position (or the last element if no index is specified)
list1.pop(2)
list1
[20]:
[0, 15, 3, 3.9, 5, 6, 7, 8, 9, 5]
[21]:
# slicing
list1[1:3]
[21]:
[15, 3]
[22]:
# slicing with skipping
list1[::2]
[22]:
[0, 3, 5, 7, 9]
[23]:
# length of list
len(list1)
[23]:
10

Tuple#

A tuple is similar to a list but is immutable, meaning its elements cannot be changed once assigned.

[24]:
# definition
tuple1 = (1, 2, 3, "Python", 4.5)
print(tuple1)
(1, 2, 3, 'Python', 4.5)
[25]:
# access with index
print(tuple1[0])
1

Set#

A set is an unordered collection of unique elements. Sets are mutable but do not allow duplicate elements.

[26]:
set1 = {1, 2, 3, "Python", 4.5, 2}
print(set1)  # Output: {1, 2, 3, "Python", 4.5}
{1, 2, 3, 'Python', 4.5}
[27]:
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
[28]:
# Union
print("Union:", set1 | set2)
print("Union:", set1.union(set2))
Union: {1, 2, 3, 4, 5, 6, 7, 8}
Union: {1, 2, 3, 4, 5, 6, 7, 8}
[29]:
# Intersection
print("Intersection:", set1 & set2)
print("Intersection:", set1.intersection(set2))
Intersection: {4, 5}
Intersection: {4, 5}
[30]:
# Difference
print("Difference (set1 - set2):", set1 - set2)
print("Difference (set1 - set2):", set1.difference(set2))
Difference (set1 - set2): {1, 2, 3}
Difference (set1 - set2): {1, 2, 3}
[31]:
# Symmetric Difference: elements that are in either of the sets and not in their intersection
print("Symmetric Difference:", set1 ^ set2)
print("Symmetric Difference:", set1.symmetric_difference(set2))
Symmetric Difference: {1, 2, 3, 6, 7, 8}
Symmetric Difference: {1, 2, 3, 6, 7, 8}
[32]:
# Adding an element
set1.add(6)
print("Set 1 after adding 6:", set1)
Set 1 after adding 6: {1, 2, 3, 4, 5, 6}
[33]:
# Removing an element
set1.remove(6)
print("Set 1 after removing 6:", set1)
Set 1 after removing 6: {1, 2, 3, 4, 5}

Dictionary#

A dictionary is an unordered collection of key-value pairs. Each key must be unique and immutable, while values can be of any data type.

[34]:
# definition
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(dict1)
{'name': 'John', 'age': 30, 'city': 'New York'}
[35]:
# access value via key
print(dict1["name"])
John
[36]:
# modify value via key
dict1["age"] = 31
print(dict1)
{'name': 'John', 'age': 31, 'city': 'New York'}

Control Statements#

  1. if-else condition statement

  2. while loop

  3. for loop

if-else#

The if statement is used to test a condition. If the condition is true, a block of code is executed. The else statement can be used to execute a block of code if the condition is false.

[37]:
x = 10
if x > 100:
    print("x is greater than 100")
elif x < 10:
    print("x is less than 10")
else:
    print("x is between 10 and 100")
x is between 10 and 100

while#

The while loop is used to repeat a block of code as long as a condition is true.

[38]:
count = 0
while count < 5:
    print(count)
    count += 1
0
1
2
3
4

for loop#

The for loop is used to iterate over a sequence (like a list, tuple, dictionary, set, or string) and execute a block of code for each element.

[39]:
for i in range(5):
    print(i)
0
1
2
3
4

Simple I/O Streaming#

Input#

name = input("Enter your name: ")
print("Hello, " + name + "!")

Formatted Output#

String#

[40]:
name = "John"
age = 30

# Example of formatted output using format() method
print("Name: {}, Age: {}".format(name, age))
Name: John, Age: 30
[41]:
# Example of formatted output using f-strings
print(f"Name: {name}, Age: {age}")
Name: John, Age: 30

Float#

[42]:
price = 10.99
# built-in round function
print(f"The price of the item is {round(price, 1)}.")
The price of the item is 11.0.
[43]:
# f-string
print(f"The price of the item is {price:.1f}.")
The price of the item is 11.0.