Basics of Python

Basics of Python

By Atharva Gangji

Python is an interpreted, high-level and general-purpose programming language. Python was conceived in the late 1980s by Guido van Rossum at Centrum Wiskunde & Informatica (CWI) in the Netherlands as a successor to ABC programming language.

  • Choose an IDE

An integrated development environment is a software application where you can write your code easily.

There are many IDE's like -

  1. Pycharm - [ jetbrains.com/pycharm ]
  2. Visual Studio Code - [ visualstudio.microsoft.com ]

Tutorial - [ youtu.be/HJj9CP-qEIM ]

  • Get Started

Let's start with typing something on the screen. To type something you use the Print command.

print("Hello World!")

print("My name is Atharva")
  • Variables

Variables are containers where we can store pieces of data we want to work with in our programs.

This is how you create a variable.

character_name = "George"
character_age = "70"

This is how you add variables to a sentence.

print("There once was a man named " + character_name)
print("He was " + character_age + " years old")
print("He really liked the name " + character_name)
print("But didn't like being " + character_age)

Tutorial - [ youtu.be/gmiB4xQ-BKw ]

  • Getting User Input

You can get input from the user, for ex. their Name.

name = input("Enter your name: ")
print("Hello", name + "!")

The word input is which that gets the input the user

Tutorial - [ youtu.be/1gEZi0uJ3sw ]

  • Lists

An array is a container which acts a lot like the variables we've seen so far in this course, except they allow us to store more than one piece of data.

lucky_numbers = [4, 8, "fifteen", 16, 23, 42.0]

Tutorial - [ youtu.be/c2C7bvkVGbI ]

  • Comment

A comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.

To create a comment you have to put a # at the beginning

# This is a comment
  • Tuples

Tuples are used to store multiple items in a single variable.

Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.

A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets.

thistuple = ("apple", "banana", "cherry")
print(thistuple)

Tutorial - [ youtu.be/DehzAA0ZIhA ]

  • Functions

~ Creating a Function

In many programs there will be certain functionality that needs to be re-used multiple times.

def sayHi():
    print("Hello")

~ Calling a Function

To call a Function we simple state the Function's name and then directly after, place an open and closed parenthesis. The open and closed parenthesis tells Python that you want to actually execute the Function.

sayHi()

Tutorial - [ youtu.be/LbOwv6y6xjo ]

  • If Statements

With if statements, our programs are able to become a lot smarter and we're able to do more things with them (which is always a good thing)!

As our programs get more complex, we'll be dealing with more and more data, and we'll want to use that data to inform the program on what it should do in certain situations.

Tutorial - [ youtu.be/V1w7QAUVqQI ]

is_student = False
is_smart = False

if is_student and is_smart:
  print("You are a student")
elif is_student and not(is_smart):
  print("You are not a smart student")
else:
  print("You are not a student and not smart")


# >, <, >=, <=, !=, ==
if 1 > 3:
  print("number omparison was true")


if "dog" == "cat":
  print("string omparison was true")
  • Dictionaries

Dictionaries are Python's implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.

Tutorial - [ youtu.be/dKg5hzE0I00 ]

test_grades = {
    "Andy" : "B+",
    "Stanley" : "C",
    "Ryan" : "A",
    3 : 95.2
}

print( test_grades["Andy"] )
print( test_grades.get("Ryan", "No Student Found") )
print( test_grades[3] )
  • While Loops

It basically determines whether or not we can keep looping again.

Tutorial - [ youtu.be/Ghz4YwOXtTA ]

index = 1
while index <= 5:
  print(index)
  index += 1