Using Python

23
Using Python or how to code snake language in windows

Transcript of Using Python

Using Pythonor how to code snake language in windows

About Python…

Python is a common programming language…

It is used for a large variety of applications, for example, analytical software.

Codeimport os

x = os.getcwd() #Assigns the current directory to xprint(x)

-------------------------------------------------------------------------------------OutputC:\Users\Sebastian\Downloads

A simple Python program looks like above.

Explanation time…

Codeimport os

x = os.getcwd() #Assigns the current directory to xprint(x)

-------------------------------------------------------------------------------------OutputC:\Users\Sebastian\Downloads

The purples are statements. The import statement tells the computer to grab a prewritten piece of code and implement it easily…

Codeimport os

x = os.getcwd() #Assigns the current directory to xprint(x)

-------------------------------------------------------------------------------------OutputC:\Users\Sebastian\Downloads

The greens are modules. Modules are a way to write code so that you can implement it easier in other parts of your code…

The reds are variables. The x is here assigned to the value of os.getcwd(). Variables are a way to store data that will be used later, either, for example, for calculations, or for text manipulation…

Codeimport os

x = os.getcwd() #Assigns the current directory to xprint(x)

-------------------------------------------------------------------------------------OutputC:\Users\Sebastian\Downloads

Codex = ‘Hello you’ #x is now a string-variabley = 7 #y is now a number-variableprint(x)print(y)

-------------------------------------------------------------------------------------OutputHello you7

Here’s a visual demonstration of different kinds of variable values and how the values are stored to a variable. The ‘Hello you’ is a string, and the 7 is an integer, shortened int…

Codeimport os

x = os.getcwd() #Assigns the current directory to x “””print(x)

-------------------------------------------------------------------------------------OutputC:\Users\Sebastian\Downloads

The grey are comments. The # represents a single line comment, which is used when there needs to be a quick explanation, such as above. There is also the “””…“”” which is a multiline comment, mostly used to comment away sections of code that isn’t used. The comments does nothing when the program runs…

Codeimport os

x = os.getcwd() #Assigns the current directory to xprint(x)

-------------------------------------------------------------------------------------OutputC:\Users\Sebastian\Downloads

And finally, the blues are functions. Functions is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing…

Now it’s time to install Python…

Start by going to https://www.python.org/downloads/...

Then click the download button for the version of Python you want, we are using Python 3.4.* for the code in this presentation…

Finally it is just a matter of following the instructions given.

Even though Python has a large built in library, it lacks certain modules to ease the code-writing…

Therefore, we’ll now guide you through the process of installing more libraries to Python…

Start by downloading the library you want to install, we’ll install the wikipedia library in this tutorial…

If there is a .exe file to download, download and run it, and it’s installed. Otherwise, download the .gz.tar file and unzip it to the folder where Python is, using e.g. WinRAR…

This is what my Python folder looks like.

After that it’s time to start the old CMD. When it’s started, navigate to the Python folder, it should look something like the picture above…

Now it’s time to install the library. Type in the code shown above in to CMD to start the installation, but remember to type in the name of the library you’re installing instead of wikipedia…

Python –m pip install wikipedia

That’s it!By Sebastian Grunditz and Samuel Håkansson