How to Get Started With Python in 10 Easy Steps.

by sorakf17 in Circuits > Computers

285 Views, 4 Favorites, 0 Comments

How to Get Started With Python in 10 Easy Steps.

1_RJMxLdTHqVBSijKmOO5MAg.jpg

In the world of coding, there are various coding languages such as Java, Python, C++, etc. Python is one of the most popular starting programming languages that people use to learn basic coding. The language is easily understandable because of its simple syntax and code. Python is primarily used for machine learning and data analysis.

Installing Python

Screenshot 2022-04-25 004412.png

To install Python, go to the website https://www.python.org/.

Hover over the downloads option on the drop down menu and choose Windows, macOS, or Other Platforms. Once you have chosen the specific platform, go to the latest released version of Python and click download.

Starting a Python Project

Screenshot 2022-04-25 010321.png

To start Python, go to your search bar and open up IDLE. Python's IDLE is an integrated development environment where all the coding occurs. From the IDLE Shell, click File and then click New File, or use the hotkey Control+N to create a new file.

Primitive Data Types

Screenshot 2022-04-25 012523.png

Now that you have Python setup, you can start learning some primitive data types.

These types consist of:

  • Integer
  • An Integer is a whole number.
  • String
  • A String consists of alphanumerical numbers that may or may not have meaning.
  • Float
  • A Float is a number that includes a decimal.
  • Boolean
  • A Boolean is either true or false.



Collection Types

Screenshot 2022-04-25 025104.png

Collection Types can contain all forms of data.

Collection Types include:

  • List
  • A List is a data structure that is mutable and stores items in a variable. Lists can be created by placing various elements separated by commas inside square brackets. A List can contain any type of data such as Strings, Integers, etc.
  • Tuple
  • A Tuple, like a List, is a data type that stores multiple items in a single variable but is immutable. Tuples can contain any object. Tuples store data the same way as Lists but with parenthesis instead of square brackets.
  • Set
  • A Set is a mutable data type used to store multiple elements in a single variable. A set can be recognized by items inside curly braces separated by commas. There can not be any repeated elements in a set.
  • Dictionary
  • A Dictionary stores data values using key-value pairs. Each key in the dictionary pairs with a value. Dictionaries are created by placing the key-value pair inside curly braces. Dictionaries are unordered and mutable.


Iterations

Screenshot 2022-04-25 180732.png

Iteration in Python is the process when a sequence or code gets repeated until it is told to stop.

For loops and while loops are things that use iteration.


For Loop:

  • Repeats a sequence of code for a number of times.
  • The number of iterations is already known and is utilized to acquire a specific result.

While Loop:

  • Repeats a sequence of code for an unknown number of times until a condition is met, primarily based on a given Boolean condition.

Import Modules

Screenshot 2022-04-25 024122.png

Python contains many modules. Modules are files that contains code and sets of functions that you can apply in various cases. To call a Module, the function, Import is used.

One of the very first Modules learned in Python is the Random Module. Random allows you to simulate the experience of choosing random.

random.choice() is a function from the Random Module that generates a random element from the given collection type. By placing the collection type inside the parenthesis, a random item gets generated.

Creating Functions

Screenshot 2022-04-25 181216.png

What is a function?

  • Functions are an essential component in learning how to use Python. Functions group statements of code together to perform a specific task. This helps organize the program into smaller parts and creates a manageable workspace.
  • Functions can be created in the IDLE Shell but is mainly used in the IDLE file created from the Shell.

Creating a function:

  1. To create a function, type def.
  2. By typing def, Python knows that the user will be creating a function.
  3. Now, type the function name followed by parentheses and a colon.
  4. Inside the parentheses, a parameter(a variable used as a placeholder for actual values in the function) can be placed if needed.
  5. Enter the code under the function name to make your function do what you want it to do.
  6. Using return or print() at the end of the function can be used to obtain an output if needed.
  7. After you finish writing the function, type the function name with parentheses after it to see the result in the IDLE Shell or by typing it into the Shell each time.

Running Program

Screenshot 2022-04-25 181522.png

Running a Program in Python:

  1. At the top of your IDLE file, hover over Run and click Run Module or use the hotkey f5 to run the module.
  2. If the file has not been saved or named, Python will prompt you to create a name for the file to save it.
  3. Once this is done, the file can be run.

Error Handling

Screenshot 2022-04-25 183029.png

Error Handling is done when a code is incorrect, causing the program to stop and create an error. By handling the error, the process can continue without the program crashing.

Errors can be handled by using a try and except statements.

The try statement looks at the code and tests it out.

The except statement is where an exception can be made if there is an error by using various exceptions such as ImportError, ValueError, TypeError. These exceptions can be used to pass through errors that may be created in your code.


Congratulations

Good job, you have now learned the basics of the programming language Python.