Python Introduction - Katsuhiko Matsuda & Edwin Cijo - Basics

Introduction: Python Introduction - Katsuhiko Matsuda & Edwin Cijo - Basics

Hello, we are 2 students in MYP 2. We want to teach you basics of how to code Python.

It was created in the late 1980s by Guido van Rossum in the Netherlands. It was made as a successor to the ABC language. Its name is "Python" because when he was thinking about a Python (snake), he was also reading, "Monty Python's Flying Circus". Guido van Rossum thought that the language would need a short, unique name, so he chose Python.

Supplies

Computer and python coding programme or website (Recommended: repl.it)

Step 1: Comments/Hashtags

Comments are side notes that can be used in Python. They can be used as:

  • sidenotes
  • instructions
  • steps etc

Comments don't have any output.

#Coding

Step 2: Print and Input Statements

Print Statements

Print statements, written as print, are statements used to print sentences or words. So for example:

print("Hello World!")

The output would be:

Hello World!

So you can see that the print statement is used to print words or sentences.

Input Statements

Input statements, written as input, are statements used to "ask". For example:

input("What is your name?")

The output would be:

What is your name?

However, with inputs, you can write in them. You can also "name" the input.

Like this:

name = input("What is your name?")

You could respond by doing this:

What is your name? Katsuhiko

Then you could add a if statement to add something to the data found.

You will learn how to use them in Step 4.

Step 3: F Strings

print(f"")

The output right now, is nothing. You didn't print anything. But say you add this:

print(f"Hello {name}!")

It would work, only if the name was named. In other words, say you had a input before and you did this to it:

name = input(What is your name?)

Then the f string would work. Say for the input, you put in your name. Then when the print statement would print:

Hello (whatever your name was)!

Another way you could do this is with commas. This won't use an f string either. They are also similar. So how you would print it is like this:

name = input()

print("Hello ", name, "!")

Step 4: If, Else If (Elif), Else Statements

My code with different names using If, Else If (Elif), Else Statements.

If Statements

If statements, printed as if, are literally as they are called, if sentences. They see if a sentence equals or is something to an object, it creates an effect to something. You could think an if statement as a cause and effect. An example of a if statement is:

name = input("What is your name?")
#asking for name
if name == "JBYT27":
  print("Hello Administrator!")

The output would be:

What is your name? Katsuhiko
Hello Administrator!

However, say that the answer was not Katsuhiko. This is where the else, elif, try, and except statements comes in!

Elif Statements

Elif statements, printed as elif are pretty much if statements. It's just that the word else and if are combined. So say you wanted to add more if statements. Then you would do this:

if name == "Katsuhiko":
  print("Hello Administrator!")
elif name == "Coder":
  print("Hello Coder!")

It's just adding more if statements, just adding a else to it!.

Else Statements

Else statements, printed as else, are like if and elif statements. They are used to tell the computer that if something is not that and it's not that, go to this other result. You can use it like this (following up from the other upper code):

if name == "Katsuhiko":
	print("Hello Administrator!")
elif name == "Squid":
	print("Hello Lord Squod!")
else:
	print(f"Hello {name}!")

Step 5: Common Modules

Common modules include:

  • os
  • time
  • math
  • sys
  • replit
  • turtle
  • tkinter
  • random
  • etc.

So all these modules that I listed, i'll tell you how to use, step by step). But wait, what are modules?

Modules are like packages that are pre-installed in python. You just have to fully install it, which is the module. So like this code:

import os

When you do this, you successfully import the os module! But wait, what can you do with it? The most common way people use the os module is to clear the page. By means, it clears the console (the black part) so it makes your screen clear. But, since there are many, many, many modules, you can also clear the screen using the replit module. The code is like this:

import replit

replit.clear()

But one amazing thing about this importing is you can make things specific. Like say you only want to import pi and sqrt from the math package. This is the code:

from math import pi, sqrt

Let me mention that when you do this, never, ever add an and. Like from ... import ... and .... Just don't do it :)

Next is the time module:
You can use the time module for:

  • time delay
  • scroll text

Next is tkinter, turtle

You can use the tkinter module for GUI's (screen playing), you can import it in a normal python, or you can do this in a new repl. You can use the turtle for drawing, it isn't used much for web developing though. The math and sys The math is used for math calculations, to calculate math. The sys is used for accessing used variables. I don't really know how I could explain it to you, but for more, click here Random The random module is used for randomizing variables and strings. Say you wanted to randomize a list. Here would be the code:

import random

a_list = ["Katsuhiko","pie","cat","dog"]

random.choice(a_list)

The output would be a random choice from the variable/list. So it could be pie, Katsuhiko, cat, or dog. From the random module, there are many things you can import, but the most common are:

  • choice
  • range
  • etc.

That's it!

Step 6: First Game! Using the Random Module

Now you will create your first game using the random module.

Firstly we import the random module

Then we have to write this:

import random

num2 = random.randint(1,100) #This means that the numbers will be chosen from 1-100, you can change if wanted
guesses = 10 #This is how many guesses the player gets

Then we print the title (Number Game!)

Then we go into something new called, While True:. This statement will allow the code to continuously loop.

Then we add the input statement:

num = int(input("Guess a number 1-100\n: ") #The \n means going to the next line

We add the int before the question to make it an integer answer allowing us to differentiate and do math things with num2 and num. This input question also should be inside the While True:.

Then we say that if num is bigger then num2 then say that its too high and it will say how many guesses you have left like this:

if num > num2:
print(f"Too high. You have {guesses} guesses left")
guesses-=1

Then you do the same thing but then the other way around in a second if (still inside the while loop).

if num < num2:
print(f"Too low. You have {guesses-1} guesses left") guesses-=1

Then you add both if guesses goes to 0 then you lose and if num = num2 then we win

if num == num2:
print(f"You got it right! You finished with {guesses-1} guesses left")
break # The break means the code stops.
if guesses == 0:
print(f"You lost! The correct number was {num2}")
break

This is all of the code for the number guessing game.

All the code together should be like this:

print("Number Game!")
while True:
num = int(input("Guess a number 1-100\n: "))

if num > num2:
print(f"Too high. You have {guesses-1} guesses left")
guesses-=1
if num < num2:
print(f"Too low. You have {guesses-1} guesses left")
guesses-=1
if num == num2:
print(f"You got it right! You finished with {guesses-1} guesses left")
break
if guesses == 0:
print(f"You lost! The correct number was {num2}")
break

This my remixed version of the Number Game:

https://repl.it/@YeeTEDWIN/Number-Guessing-Game#ma...

The remixed version has difficulty levels and other secrets.

Hooray! We made it through without sleeping!

Thanks for seeing our instructable. Hope you learned something new. :)

The next step is a more advanced game. The next step will explain each section of the code to understand what you are doing. This is optional

Step 7: Brutal Force Password Cracker

import random
characters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0','!','@','#','$','%','^','&','*','(',')','-','_','+','=','~','`']
cha = ''
ba=[]
for item in characters:
  cha+=item
print("characters: "+cha) 

The code above is the code to write all the characters that can be used in the password

password = input("Enter a four digit password. ").lower()
guessing = True
x=0
q = 11
w=11
e=11
r=11
tens = 0
ones =1 
hundreds = 0
thousands = 0
while guessing:
  r+=1
  x+=1
  if r == 62:
    e+=1
    r=11
  if e == 62:
    w+=1
    e=11
  if w == 62:
    q+=1
    w=11

  guess =''
  a = characters[q-11]
  b = characters[w-11]
  c = characters[e-11]
  d = characters[r-11]
  guess +=a
  guess+=b
  guess+=c
  guess+=d

The code above shows the guessing process and how to find every possible 4 digit password with the characters

  if guess == password:
    print("Guess number "+str(x))
    print("Guess: "+guess)
    break
  else:
    print("Guess: "+guess)

The code here shows the amount of passwords it inspected to find the "password" you wrote.

Here is the link of the Brute Force Password Cracker:

https://repl.it/@YeeTEDWIN/Brutal-Force-Password-C...

It takes a total of 7171112 guesses to crack """".

Be the First to Share

    Recommendations

    • Anything Goes Contest

      Anything Goes Contest

    Comments