Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • mfb23137/cs101-csai
  • gvb23154/cs101-csai
  • kkb23140/cs-101-cognitive-science-and-artificial-intelligence-sem-2-first-half
  • kxb23169/cs101-csai
  • xgb21195/cs101-csai
5 results
Show changes
# Python Quick-Start Guide
## What is Python?
Python is a high-level, interpreted programming language. Most of you should be familiar with Java, Python and Java are similair in the sense that they are both high-level. However, Java is a compiled language (meaning that it is turned into CPU instructions before being run) while Python is an interpreted language. Java is also statically typed (Meaning you have to assign a variable type explicitly) while Python is dynamically typed (Python itself decides which variable type you want to use)
## Downloading Python
You can go to [python.org](https://www.python.org/) to download the latest version of Python for your system, installation is pretty simple you just double click the downloaded file and follow the instructions on-screen.
## But how do I begin?
You don't *really* need anything other than a text editor to begin developing in Python. In the end as long as you have a text file with the correct text in it (and the correct file extension) the python interpreter will be able to run it which you can do from the terminal like so:
![Python running in the Command Line](https://gitlab.cis.strath.ac.uk/xgb21195/cs101-csai/-/raw/main/img/pythoncmd.png?ref_type=heads)
## Ok but... I don't want to use Notepad for this
That's fair enough, just because you *can* do something doesn't mean you *should*. Editing with Notepad is a hassle that you probably don't need. Fortunately, Python has many IDEs (Integrated Development Environments, Think Eclipse, NetBeans, IntelliJ from your Java classes). Here are a couple that I know about, but if you Google around I'm sure you can find more.
### IDLE
IDLE is a very, very simple IDE that comes with every Python installation, if you have downloaded Python then you have downloaded IDLE. It gives you some basic IDE features like syntax highlighting and running the code but it's not that useful for anything more than learning to code.
![IDLE running on Windows](https://gitlab.cis.strath.ac.uk/xgb21195/cs101-csai/-/raw/main/img/idlewin.png?ref_type=heads)
### Visual Studio Code (VSCode)
VSCode is a code editor developed by Microsoft that is known for it's wide compatibility with lots of different programming languages. It's core strength is consistency when developing in lots of different languages at once but it suffers from not being specialised.
![VSCode Running on Windows](https://gitlab.cis.strath.ac.uk/xgb21195/cs101-csai/-/raw/main/img/vscodewin.png?ref_type=heads)
#### To get set up on Python in VSCode:
- Install Python:
- Ensure Python is installed on your system.
- [python.org](https://www.python.org/)
- Install VS Code:
- Download and install Visual Studio Code.
- [code.visualstudio.com](https://code.visualstudio.com/download)
- Open VS Code:
- Launch the application.
- Install the Python Extension:
- Go to the Extensions view by clicking on the square icon on the sidebar
- Search for 'Python'.
- Install the Python extension by Microsoft.
- Open Your Python File:
- Open the folder containing your script or create a new Python file.
- Run the Script:
- Right-click in the editor and select "Run Python File in Terminal"
- or
- Use the play button on the top-right corner of the editor. This will execute the script in the integrated terminal.
### PyCharm
PyCharm is a highly advanced IDE created by JetBrains. It will be very familiar to those of you who are used to working with IntelliJ, it has a lot of features designed to make developing in Python easier (such as more powerful AutoComplete, integration with Git). [As Strathclyde Students, you even get access to the more powerful version of it for free](https://www.jetbrains.com/community/education/)!
Getting started is very simple, make sure you have Python installed go to [jetbrains.com/pycharm](https://www.jetbrains.com/pycharm/download/) and download it from there.
Once it's installed you can just follow the on-screen instructions to make a new project and press on the green play-button to run it.
# Using Python
I'm gonna throw up a second file that goes through basic Python syntax very soon, either later this evening or tomorrow morning. In the meantime I hope this small write up and the video have helped you in some way. Good luck with the labs, if you have any questions:
[pat.prochacki@strath.ac.uk](mailto:pat.prochacki@strath.ac.uk)
# Python Syntax vs Java
Python and Java are very different when it comes to coding within them. Python is a lot less structured, this file will go over similarities and differences between the two to hopefully help you guys out!
## Hello World
### Python
```python
print("Hello World")
```
### Java
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
```
As you can see, Python is a lot simpler as you can just "start" coding without having to do all the surrounding method declarations and things like that.
You *can* have a main method in python, if you'd like, though that tends to be more useful for larger programs that use multiple files. Here's how that's done:
```python
def main():
print("Hello World")
if __name__ == "__main__":
main()
```
## Variable Declarations
Java:
```java
String name = "Alice";
int age = 30;
```
Python:
```python
name = "Alice"
age = 30
```
Note: In Python, variables are not explicitly typed. The interpreter infers the type dynamically.
## Basic Data Types
Strings: 'hello', "world"
Integers: 42, -1
Floats: 3.14, -0.001
Booleans: True, False
## Control Structures
### If-Else
Java:
```java
if (age > 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
```
Python:
```python
if age > 18:
print("Adult")
else:
print("Minor")
```
Note: Python uses indentation instead of curly braces to define blocks.
## Loops
For Loop (Java):
```java
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
```
For Loop (Python):
```python
for i in range(5):
print(i)
```
While Loop (Java):
```java
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
```
While Loop (Python):
```python
i = 0
while i < 5:
print(i)
i += 1
```
5. Functions
Java:
```java
public static int addNumbers(int a, int b) {
return a + b;
}
```
Python:
```python
def add_numbers(a, b):
return a + b
```
Note: Python functions start with the def keyword. No need to specify return types.
## Lists and Dictionaries
List (Similar to Java's ArrayList):
```python
numbers = [1, 2, 3, 4, 5]
```
Dictionary (Similar to Java's HashMap):
```python
person = {"name": "Alice", "age": 30}
```
## Comments
Java:
```java
// This is a single line comment
/* This is a multi-line comment */
```
Python:
```python
# This is a single line comment
```
Note: Python does not have a native syntax for multi-line comments, but you can surround the comment in quoutes as a workaround