Python Programmer Logo Python Programmer Python Programmer Logo


Python Basics Series: Tutorials for Beginners

Below are the python files I used in these videos and the actual videos


The video files

File Download Date Modified
Tutorial - part 1 Download File 7 Feb 2024
Tutorial - part 2 Download File 10 Feb 2024
Tutorial - part 3 Download File 22 Feb 2024

The actual code in each file

Tutorial - part 1


"""This tutorial will show a lot of basic input/output functions and methods so that
you can start your python programming journey. It wil focus on efficiency and won't
be too complex, there will be more advanced tutorials on my channel in future, so be
sure to like and subscribe."""

#The first in-built function you need to know is print
print("Hello world")
"""This will allow you to display a piece of text of your choosing to the screen,
in this app it will display to the window on the right -->"""

#Now I will run the program

#So now let's do something a bit more advanced

#let's say we want to store something in memory then print it

#We use a variable, you can name it whatever you want, but it should be descriptive

name = "John"
"""That is one example of a variable, now we can use it to print the name of someone,
however, it doesn't need to be the only thing in the print statement, you can use a comma,
to seperate the name and any text you want to put in as well"""

print("My name is:",name)

#Okay, so we printed out some text, we can also print out numbers

age = 7

#Now let's try to print this age directly

print("My age is:",age)

#However, what if we want to add 2 numbers and print the result

num1 = 23
num2 = 5

print(num1+num2)

#This time the numbers will be in quotes

num3 = "3"
num4 = "5"

#Write in the comments what you think will happen

#Anyways, let's print out the 2 numbers
print(num3+num4)

#We can use another function 'int' to convert them to numbers
print(int(num3)+int(num4))

#So that was output, what about input, when we want to enter a value

#There is another function for this, 'input'

text = input("Enter something: ")
print("you entered:",text)

#We can also put some text inside the input function

"""There are also other ways of inputting text and you can input mnore than just text,
however that is more advanced and requires more tools, we will explore them in
future tutorials, anyways let's try to input a number"""

num = int(input("Enter a number: ")) #We can use int to convert them to numbers
otherNum = int(input("Enter another number: "))
print("The sum of the numbers is:",(num+otherNum))

"""However, a comma is usually better as it automatically adds a space between text in
quotation marks and a variable"""

"""Now we can put everything together to make something"""

name = input("Enter your name: ")
age = int(input("Enter your age: "))
anotherAge = int(input("Enter someone else's age: "))

print("Your details are:\nYour name is:",name,"\nYour age is:",age,
      "\nYou are",(age-anotherAge),"years older than this other person")

Tutorial - part 2


#Today we will be looking at String methods and conditional statements

"""This program will closely examine some of the various string methods and also
will look at conditional statements (if, elif, else) and also the boolean operators"""

#below is an example of a string
myString = "Hello, This is a string!"

#First we'll start with string slicing
print(myString[:5]) #This will print out the first 5 characters of 'myString'
print(myString[10:]) #This will print from character 10 till the end
print(myString[6:11]) #This will print from character 6 until character 10 (not including 11)

#You can also get a single character from a string, this time we'll ask the user
character = int(input("Enter a character number (index) to retrieve from string: "))
print("The character is:",myString[character])

#Okay, so now we will use one of the many string methods

print(myString.count('s'))
#This will print out the amount of times a certain character appears in our string

#Now we can use a different method
newString = myString.replace('Hello', 'Bye')
print(newString)
#So this will replace any substring with another substring, you have to store it as a new string

#There is also a 'lower' method and 'upper' method
print(myString.lower())
print(myString.upper())

#Now moving onto if statements (conditional statements)
if myString.islower() | myString.isupper():
    print("String is uniform")
else:
    print("String is irregular")

#Let's do an example with numbers
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))

if (num1 > 5) & (num2 <= 5):
    print("Success")
elif (num1 == 5) | (num2 > 5):
    print("Neither a fail nor a success")
else:
    print("Fail")

otherString = input("Enter a string to compare to myString: ")

if (myString.islower() | myString.isupper()) & (otherString.islower() | otherString.isupper()):
    print("Both strings are regular")
elif not (myString.islower() | myString.isupper()) and not (otherString.islower() | otherString.isupper()):
    print("Both strings are irregular")
else:
    print("The strings aren't similar in terms of case")

if myString[:5].lower() in otherString.lower():
    print("Both are greetings!")
else:
    print("One of the strings is not a greeting")

if myString == otherString:
    print("The strings match!")
else:
    print("The strings don't match")

Tutorial - part 3


  """Ok so I've explained a bit about iteration loops and file handling, now we will
move on into the coding examples, be sure to follow along if you're watching
from home and want to learn python"""

"""#In a for loop, we don't need any pre-defined variables
for num in range(5):
    print("The number is:",num)
    #This will print out the numbers from 0 to 4 (not including 5)

#For loops are very useful, but only if we know the amount of times we're looping
for i in range(1,6):
    #here we are looping from 1 to 5 (not including 6)
    print("I have",i,"apples")

#Anyway now let's look at counting backwards
for count in range(5, 0, -1):
    #-1 will allow us to go backwards by 1 each time
    print("Numbers till countdown ends:",count)

#We can also loop in increments, for example every even number (up to a point)
for number in range(0, 22, 2):
    #This will print out every even number from 0 to 20
    print("The even number is:",number)

#The final thing we can do with for loops is loop over data structures such as lists

#First we will create the list
myList = list(range(0,10)) #This is a handy trick for creating a list quickly

#Now we will print each number in the list
for item in myList:
    print("The item is:",item)

#Now moving onto while loops, for these we do need pre-defined variables

continueLoop = True

while continueLoop:
    continueLoop = input("Should the loop continue? ")
    if continueLoop.lower() == "yes":
        continueLoop = True
    else:
        continueLoop = False
    #Here we took a user input then assigned values to it using an if statement
    #If you haven't already, check out the previous tutorial where we went over if statements and string methods

#Now we move onto file handling, this also uses loops

#First we will read a file using a for loop

#We need to open the file and we use the "open" command for this
thisFile = open("new.txt")
#This will open the file in read mode that is called "new.txt", the file format does have to be there

#Now we can print each line of the file
for line in thisFile:
    print(line)

#Another way to open a file is to store its name in a variable
fileName = "newer.txt"
file = open(fileName)

#There are also other methods to read from the file
fileContents = file.readlines()

for line in fileContents:
    #There is also another method (split) which can split a line by any character
    line = line.split(",") #In this example, we have a comma seperated list on each line
    #This method will return an array
    #Now we can use a for loop to iterate through each item in the line list/array
    for item in line:
        print(item.strip())"""

#Now we will write to a new file, you don't need an existing file for this
fileName = input("Enter a name for the file: ")
#We have to append the txt extension to the filename, otherwise it won't work
fileName += ".txt"

#Whoops, almost forgot to open the file
myFile = open(fileName, 'w') #We need the 'w' to be able to write to the file

#Now we can use a while loop to let the user keep adding to the file
moreLines = True #remember, we need a pre-defined variable

while moreLines:
    line = input("Enter a line to add to the file: ")
    moreLines = input("Do you wish to add more lines(Y/N)? ")
    if moreLines.upper() != "N":
        moreLines = True
    else:
        moreLines = False
    #Now we can write the line to the file
    myFile.write(line)
    myFile.write("\n")
#Finally, we have to close the file
myFile.close()