Re-skill yourself!
The meteoric rise of ChatGPT and the upcoming Google Bard could be just the beginning of the new AI-powered era that may fundamentally change how we live our lives. And I believe this is the perfect time to learn Pythin and re-skill ourselves!
So, recently, I feel a strong need to re-skill myself and it’s highly technologically motivated. Given the sudden rise of ChatGPT and Gaogle Bard, I couldn’t resist but to at least start mastering the very basics of Python 3, one of the hottest languages at the moment all thanks to its rich resources, libraries, and frameworks for Artificial intelligence, aka AI.
Even though my top priority for this year should be building my second Android app powered by Kotlin, Pythin’s meteoric rise in everyone’s eyes for obvious reasons gave me an unparalleled motivation to start learning it.
My plan is o start learning PyTorch as soon as I finished learning the very basics of the language. So, from this point on, I’ll keep tracking my Python-learning journey on this blog.
My environment:
OS: Windows 10
Python 3 Version: Python 3.11.2
IDE: PyCharm
Variables:
In Python, a variable is a named reference to a value stored in the computer’s memory. Variables are used to store data that can be accessed and manipulated throughout the program’s execution.
To create a variable in Python, you can simply use the assignment operator = to assign a value to a name. For example:
x = 42
In this example, x is the variable name, and 42 is the value assigned to x. Once a variable has been assigned a value, you can use its name to reference the value throughout the program.
Python variables have the following properties:
1: Naming conventions: Variable names in Python can consist of letters, numbers, and underscores. However, they must begin with a letter or underscore, and cannot be a reserved keyword in Python (such as if, for, or while).
2: Dynamic typing: Unlike some other programming languages, Python allows variables to be dynamically typed. This means that you do not need to declare a variable’s data type before using it. Instead, the data type is inferred from the value that is assigned to the variable.
3: Memory management: Python automatically manages the memory allocated to variables. When a variable is no longer needed, Python’s garbage collector will reclaim the memory it was using.
4: Mutable vs. immutable: In Python, some types of values (such as integers and strings) are immutable, meaning that their value cannot be changed once they are created. Other types (such as lists and dictionaries) are mutable, meaning that their values can be modified.
Overall, variables are an important concept in Python (and in programming more generally) as they allow you to store and manipulate data throughout the course of your program.
Another sample of variable and user input:
color = input("Enter a color: ")
plural_noun = input("Enter a plural noun: ")
celebrity = input("Enter a celebrity: ")
print("Roses are" + color)
print(celebrity + " are blue")
print("I love " + celebrity)
Output:
Enter a color: red
Enter a plural noun: wahles
Enter a celebrity: Kendall Jenner
Roses arered
Kendall Jenner are blue
I love Kendall Jenner
List:
In Python, a list is a built-in data structure used to store a collection of items in a single variable. A list is an ordered sequence of elements, which can be of different data types (such as integers, strings, or even other lists).
To create a list in Python, you use square brackets [] to enclose a comma-separated list of values. For example:
my_list = [1, 2, 3, "apple", "orange"]
In this example, my_list is the variable name, and it contains five elements: the integers 1, 2, and 3, and the strings “apple” and “orange”.
Lists in Python have the following properties:
1: Indexing: You can access individual elements of a list by their position, or index, in the list. Python uses 0-based indexing, so the first element of a list has an index of 0. For example, to access the first element of my_list, you can use my_list[0].
2: Slicing: You can also extract a subset of elements from a list using slicing. Slicing allows you to specify a range of indices, and returns a new list containing only the elements in that range. For example, to extract the second through fourth elements of my_list, you can use my_list[1:4].
3: Mutability: Lists in Python are mutable, which means that you can modify their elements. You can add or remove elements from a list, or change the value of an existing element.
4: Functions and methods: Python provides a number of built-in functions and methods that you can use to work with lists. For example, you can use the len() function to find the length of a list, or the .append() method to add an element to the end of a list.
Overall, lists are a powerful and flexible data structure in Python that allow you to work with collections of items in your programs.
List samples:
List sample 1:
friends = ["Kevin", "Karen", "Jim"]
print(friends)
Output:
['Kevin', 'Karen', 'Jim']
List sample 2:
friends = ["Kevin", "Karen", "Jim"]
print(friends[1])
Output:
Karen
List sample 3:
friends = ["Kevin", "Karen", "Jim"]
print(friends[-3])
Output:
Kevin
List sample 4:
friends = ["Kevin", "Karen", "Jim"]
print(friends[1:])
Output:
['Karen', 'Jim']
List sample 5:
friends = ["Kevin", "Karen", "Jim"]
print(friends[:-2])
Output:
['Kevin']
List sample 6:
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
print(friends[1:3])
Output:
['Karen', 'Jim']
List sample 7:
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
friends[1] = "Mike"
print(friends[1])
Output:
Mike
List Functions:
Maybe you won’t need any extra explanations, so I skip any detailed descriptions here for saving dome time.
extend()
lucky_numbers = [4,8,15,16,23,42]
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
friends.extend(lucky_numbers)
print(friends)
['Kevin', 'Karen', 'Jim', 'Oscar', 'Toby', 4, 8, 15, 16, 23, 42]
append()
lucky_numbers = [4,8,15,16,23,42]
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
friends.append("creed")
print(friends)
['Kevin', 'Karen', 'Jim', 'Oscar', 'Toby', 'creed']
insert()
lucky_numbers = [4,8,15,16,23,42]
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
friends.insert(1, "Kelly")
print(friends)
['Kevin', 'Kelly', 'Karen', 'Jim', 'Oscar', 'Toby']
remove()
lucky_numbers = [4,8,15,16,23,42]
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
friends.remove("Jim")
print(friends)
['Kevin', 'Karen', 'Oscar', 'Toby']
clear()
lucky_numbers = [4,8,15,16,23,42]
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
friends.clear()
print(friends)
[]
pop()
This one probably needs a description. What it does is delete an element at the very tail of a list.
lucky_numbers = [4,8,15,16,23,42]
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
friends.pop()
print(friends)
['Kevin', 'Karen', 'Jim', 'Oscar']
index
lucky_numbers = [4,8,15,16,23,42]
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
print(friends.index("Kevin"))
0
lucky_numbers = [4,8,15,16,23,42]
friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"]
print(friends.index("Mike"))
ValueError: 'Mike' is not in list
Tuples:
In Python, a tuple is a built-in data structure used to store a collection of items in a single variable, similar to a list. However, unlike lists, tuples are immutable, which means that their elements cannot be changed once they are created.
To create a tuple in Python, you use parentheses () to enclose a comma-separated list of values. For example:
my_tuple = (1, 2, 3, "apple", "orange")
In this example, my_tuple
is the variable name, and it contains five elements: the integers 1
, 2
, and 3
, and the strings "apple"
and "orange"
.
Tuples in Python have the following properties:
- Indexing: You can access individual elements of a tuple by their position, or index, in the tuple. Python uses 0-based indexing, so the first element of a tuple has an index of 0. For example, to access the first element, you can use
my_tuple[0]
. - Slicing: You can also extract a subset of elements from a tuple using slicing. Slicing allows you to specify a range of indices and returns a new tuple containing only the elements in that range. For example, to extract the second through fourth elements of
my_tuple
, you can usemy_tuple[1:4]
. - Immutability: Tuples in Python are immutable, which means that you cannot modify their elements. Once a tuple is created, its elements cannot be changed or deleted.
- Functions and methods: Although tuples are immutable, Python provides a number of built-in functions and methods that you can use to work with tuples. For example, you can use the
len()
function to find the length of a tuple, or thesorted()
function to create a new sorted tuple from an existing one.
Overall, tuples are a useful data structure in Python for situations where you need to store a fixed collection of elements that should not be modified. Because they are immutable, they can be used as keys in dictionaries, whereas lists cannot.
Tuples samples:
Again, you won’t need any extra explanations, so I skip any detailed descriptions here for saving some time.
List sample 1:
coordinates = (4,5)
print(coordinates[0])
Output:
4
List sample 2:
coordinates = (4,5)
coordinates[1] = 10
print(coordinates[0])
Output:
TypeError: 'tuple' object does not support item assignment
If it were a list…
coordinates = [4,5]
coordinates[0] = 10
print(coordinates[0])
Output:
10
List sample 3:
coordinates = [(4,5), (6,7), (80,34)]
print(coordinates[0])
Output:
(4, 5)