Surya Dantuluri's Blog

Introduction to Python

A simple introductory tutorial on how to use Python.

Welcome to an introductory tutorial of programming in Python.

If this is your first time touching any programming language, this tutorial will be essential for the future tutorials. If this is not your first programming language, some material may seem similar to your previous experiences in programming.

Introduction


Python unlike other popular programming languages is an interpreted language (depending on your implementation), meaning that you don't generally need to compile and run Python unlike with Java and C++. When you run a Python file, you get an output without needing to do anything else.

I don't like to learn theory before implementation, which is the case with many other students, which is why I will  show and then explain what is going on from now on.

Printing "Hello World!" and Good Code Practices


print() is a Python function. Functions are executed by computers to do something, in this case, the print() function outputted the string parameter to the terminal

function(parameter)

Calling the print() function tells the computer to execute it. Try editing the parameter above to anything you want like, "Hello , insert name".

Challenge: Print "Birds fly in the air"

Syntax

One of the reasons you can't write

print "Hello World"

is because of something called Syntax. Just like in English grammar, you c'ant p.ut pe,riods and c!om,mas a,n;yWheR.e you like, programming languages have strict rules of how you can use them so that they perform exactly how you'd expect.

Comments

If you're the next generation of coders, you need to know one thing from an experienced programmer; Comment everything. There's basically not enough commenting you can do in your code.

Essentially what commenting in programming is, is what you'd expect; comments in your code, indicating what the function or class does and what parameters it expects and what it returns. Comments also sometimes indicate what a process is doing within a function. Comments can help others debug your code as well.

This is an example of comments. Comments start with a

#

Each line that you want to comment (sometimes you may want to comment out code for debugging) needs to start with a hashtag.

Variables


Want to store something? Something like your name or a long number that you can't keep in your head for too long? Well programming can help with that. Just create a variable like this:

a = "This variable a"

Congrats! You just created your first variable in Python! Now instead of memorizing that variable a is "This is a", Python stores it to be used anytime.

In a more practical sense, storing variables becomes more useful when doing something like this:

firstNumber = 9 secondNumber = 10 thirdNumber = firstNumber+secondNumber

Instead of typing the numbers 9 and 10 another time to find out their sum, you can reference to their variables to add them up to be stored in another variable, thirdNumber.

Try changing the variable values and variable names! Note: writingLike thisIs called camel case. This is common practice when making variable and function names because it is easier to read.

Concatenating String variables

You can concatenate string variables and manipulate number with variables. Let's see that in action:

Challenge: Print "Thomas the Tank Engine is 5 Years Old" using only variables with values "Thomas the Tank", 2,3, "Years", "Engine is ", "Old".

Bonus Challenge: Add 9+10 to get 21 using variables with values 9 and 10 only.

Inputs


Be honest, this is pretty boring right?

Just writing some code and getting a plain output, right? Well not anymore with inputs. Set a variable to an input to store the input, like this:

name = input("Enter your name: ")

Variable

name

will store the input.

Now you can add inputs. Fill inputs in the program below to see the funny sentences you can make:

Challenge: Find a MadLib online and fill it in with variables that are populated with input values

Data types


Concatenation two strings will produce another string no matter of the content of the string.

a = "9" b = "10" print(a+b) #Prints 910 c = 9 d = 10 print(c+d) #Prints 19 print(a+b+c+d) #Prints 91019

You can also turn variables c and d into variables equivalent to a and b like this:

# Add c and d as strings print( str(c) + str(d) ) #Prints 910 print(a+b+str(c)+str(d)) #Prints 910910

Try it out yourself:

Challenge: Print "13131" only with variables with values of 1 and 13.

Logic Operators


Need to keep a secret? Do it with Python Equality! Set a password and only print out your secret message if the password matches with the input.

and indicates that two boolean expressions need to be true for the output to be true, otherwise it is false.
not reverses the boolean expression
or indicates that either boolean expression needs to be true for the output to be true as well.
<,>,>=,<= can also be used for number operations.

Challenge: Make a series of questions with if and else to determine if the user can access a super secret fact about yourself.

Loops


For Loop

groceryList = [ "milk", "bread", "eggs", "coffee" ] # The equivalent while loop for iterating through the list index = 0 while index < len(groceryList): # Get the item from the index item = groceryList[index] print(item) index = index + 1

This is a for loop. It recurses through the list or a set of numbers. Another for loop is:

for number in [10, 20, 100]: # number = 20 print( number / 2 )

While Loop

# Start a variable at 0 number = 0 # While the variable is less than 4 while number < 10: print(number) # Increase the variable by 1 number = number + 1

A while loop allows you to go on a loop forever as long as a certain condition is met. In this case the variable number has to be less than 10 in order to loop. This means numbers up to 9 should only be printed.

Challenge: Print the names of all your friends or family members using a for loop.

Challenge: Print the names of all your friends or family members using a while loop.

Conclusion


This is concluding the Python introductory tutorial. More tutorials are being made now.

writes articles on Machine Learning, Full Stack Development, and Insightful Topics