Sitemap
Ananya Venkatesan

Ananya Venkatesan

Intro to Python

5 min readMay 14, 2023

Hey everyone,
A short intro about me…
I’m Ananya. I’m a junior in high school, and this is my first article! I’ve been meaning to write articles on Python to help middle and high school students get exposure to Python programming for a while now. I’m also planning to write simple data science articles and machine learning models using Python later in this series.
Learning Python has been an exciting journey for me and I would like to share that with other students out there.

So, let’s start with the basics: What is Python?
Python is a scripting language and is widely used in many IT companies around the world. Python has gained a lot more popularity in Machine Learning and Artificial Intelligence due to the availability of sophisticated Python modules.

Let’s dive into action.
Installation of Python:
Let’s use Python 3.10.x version for all our studies. Here “3” refers to a major release version and “10” refers to a minor version. X refers to smaller changes to Python releases.
Before installing Python3 (which refers to major version 3), please check
what version of Python3 already exists on your Mac.


python3 — version

If you see version 3.10.x, then you are good, and there is no need to install or update.
But if you see a “command not found” message, you do not have python3
installed.
To install python3,

brew install python@3.10

Typically, the Python3 binary will get installed in the following dir.
ls -l /usr/local/Cellar/python@3.10/3.10.0_1/bin/python3.10

To access the binary, we will make a link to this binary from a
standard bin directory


ln -s /usr/local/Cellar/python@3.10/3.10.0_1/bin/python3.10/usr/local/bin/
python3

Check the version now and you should be all set to learn.
We will kickstart our learning using interactive Python; for that open a
the terminal on your Mac.
Type: python3


Python 3.10.8 (main, Oct 13 2022, 10:19:13) [Clang 12.0.0 (clang-
1200.0.32.29)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>>

The ‘>>>’ is the interactive Python prompt, waiting for you to enter the next
command.

Get Ananya Venkatesan’s stories in your inbox

Join Medium for free to get updates from this writer.

Data Types:
Python has the below basic data types to store data:
int — Number without a decimal point
float — Number with a decimal point
string — Alphanumeric and special characters
complex — Imaginary numbers
Here is an example of how variables can be created and assigned values.

>>> i = 10
>>> print(i)
10
>>> f = 22.5
>>> print(f)
22.5
>>> name = “Bill”
>>> print(name)
Bill
>>> c = 25 + 5j
>>> print(c)
(25+5j)
>>> type(c)
<class ‘complex’>
>>>

“i = 10” is precisely called an ‘assignment statement’. An integer value of
10 gets assigned to a variable name “i”. The variable name should start with an alphabet and it can be an alphanumeric. It can also have an “_” in it.


Students_count = 27

In the example above, an underscore is used and it helped add meaning to the variable. Creating a meaningful variable name is always appreciated. It makes it easier to understand what the variable is used for and what it holds.

Other Python Data Types:
list — To store an array of mutable data
tuple — To store an array of immutable data
dictionary — To store key-value pairs
set — To store a unique set of values
Here are the examples for the List data type.


>>> student_names = [‘Jack’, ‘Jill’, ‘Tom’, ‘Jerry’]
>>> print(student_names)
[‘Jack’, ‘Jill’, ‘Tom’, ‘Jerry’]
>>> student_names[0] = ‘Bob’
>>> print(student_names)
[‘Bob’, ‘Jill’, ‘Tom’, ‘Jerry’]
>>> student_names[3] = ‘Steph’
>>> print(student_names)
[‘Bob’, ‘Jill’, ‘Tom’, ‘Steph’]
>>>

In the above list example, there are 4 data in the list ‘student_names’
Each data can be accessed using an index number which is the position of
the data inside the list. The index number starts with 0. The above example
shows how the data at indexes 0 and 3 can be modified by assigning new
values.
Here are the examples for the Tuple data type:


>>> employee_names = (‘Jack’, ‘Jill’, ‘Tom’, ‘Jerry’)
>>> print(employee_names)
(‘Jack’, ‘Jill’, ‘Tom’, ‘Jerry’)
>>> employee_names[0] = ‘Moore’
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: ‘tuple’ object does not support
item assignment
>>>

List vs Tuple:
Unlike List, Tuple data was created using ‘(‘ and ‘)’ brackets. Tuple data can
be accessed using index numbers just as that of a list. However, trying to
change data results in an error. This is where the main difference between
List and Tuple come into play.
The List datatype can be modified at any time and so the list is called a ‘mutable’ data type. However, once defined, the Tuple data type cannot be modified and so it is called an ‘immutable’ data type.
Examples of the Dictionary datatype:
The Dictionary type helps to store and access key-value pairs. The key
value pair provides value to a key name. Programmers also refer this to
as name-value pairs.
Here is what Jen likes. She is 23 years old, and she likes the movie “Avatar”, the drink “milk”, and the country “USA”. Then, I can provide this data in dictionary format as below with the name “jen”.


jen = { “movie”: “Avatar”, “drink”: “milk”, “country”: “USA” }
>>> jen = { “movie”: “Avatar”, “drink”: “milk”, “age”: “23”, “country”:
“USA” }
>>> print(jen[“age”])
23
>>> print(jen[“movie”])
Avatar
>>> print(jen[“drink”])
milk
>>>

One can also get the keys and values in separate lists if needed.


>>> jen = { “movie”: “Avatar”, “drink”: “milk”, “age”: “23”, “country”:
“USA” }
>>> keys = list(jen.keys())
>>> print(keys)
[‘movie’, ‘drink’, ‘age’, ‘country’]
>>> values = list(jen.values())
>>> print(values)
[‘Avatar’, ‘milk’, ‘23’, ‘USA’]
>>>

Remember, the key should be string type but the value can be any type.
Python has another data type called ‘set’. The set allows to store a unique
set of values.
Here is an example of the data type set:


>>> jim_movies = {“Avatar”, “Iron man”, “Cars”,
“Frozen”}
>>> amy_movies = {“Avatar”, “Hulk”, “Ants”,
“Speed”, “Cars”}
>>> print(jim_movies)
{‘Iron man’, ‘Cars’, ‘Frozen’, ‘Avatar’}
>>> print(amy_movies)
{‘Ants’, ‘Speed’, ‘Cars’, ‘Avatar’, ‘Hulk’}
>>>
Let’s create a unique (with no duplicates) set of movies Jim and Amy likes
using the set type.
>>> movies=()
>>> movies = set()
>>> type(movies)
<class ‘set’>
>>> movies.update(jim_movies, amy_movies)
>>> print(movies)
{‘Ants’, ‘Avatar’, ‘Hulk’, ‘Cars’, ‘Iron Man’,
‘Frozen’, ‘Speed’}
>>>
>>> print(movies[0])
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: ‘set’ object is not
subscriptable
>>>

Note the bracket used for storing set values and that is similar to the dictionary.

The Set type does not support indexing like List does. A for loop (which we will cover in the next section) helps access the values in a set type.
I hope you enjoyed this article and found it helpful! Our next one will cover list comprehension, string indexing, slicing, and control.

--

--