Introduction to Python

Python Logo

Why Python?

Python Logo
  • Python has very easy-to-read syntax.
  • Python has been used in many different ways from scientific computing to making games.
  • Python has a very large user-base and a proven track record of 30 years.
  • Easy to install on many operating systems.

What is an IDE

  • IDE stands for Integrated Development Environment
  • It's a program that allows you to write programs, debug (fix errors) and see the output.
  • Has many tools to help you write programs easier and faster
  • IDE's have syntax highlighting - a very useful feature that color-codes parts of your program.
Replit with Doom

Doom on a Pregnancy Test

Replit

Replit is an online IDE that makes it easy for teachers and students to start programming.

Replit Logo

Data Types

These are some basic data types in Python

  • Text Types
    • String:
      my_string = "Hello World"
  • Numeric Types
    • Integer and Floats
      
                                      my_int = 5
                                      my_float = 32.4
                                  
  • Sequence Types
    
                                    my_list = ["apple", "banana", "orange", 5]
                                
  • Boolean Types
    
                                    my_boolean = True 
                                

STRINGS

What is a String?

Defintion: An immutable sequence of characters wrapped inside single, double or triple quotes

String Graphic

HELLO WORLD!

print()

The print function enables us to print strings to the console using the following syntax.


                    print("My STRING HERE")
                

                    print(f"This is {variable_1} and this is {variable_2}.")
                

An example of a Python Program


                    # Program to check if a number is prime or not
                    num = 29

                    # To take input from the user
                    #num = int(input("Enter a number: "))

                    # define a flag variable
                    flag = False

                    # prime numbers are greater than 1
                    if num > 1:
                        # check for factors
                        for i in range(2, num):
                            if (num % i) == 0:
                                # if factor is found, set flag to True
                                flag = True
                                # break out of loop
                                break

                    # check if flag is True
                    if flag:
                        print(num, "is not a prime number")
                    else:
                        print(num, "is a prime number")
                

How should I name variables?

  • Variable names can only contain letters, numbers and underscores. However you cannot start a variable with a number.
  • No spaces allowed.
  • The Python language has several "reserved" words used by other functions. An example is something like print
  • Not a rule but you should aim to use names that describe the variable.
  • One should also try to stick with all lowercase characters.