Python for Beginners (Part 1)

Python for Beginners (Part 1)

Want to build a slick website? Develop a video game? Or maybe create an artificially intelligent model? Python's got you covered! Python is a high-level programming language, with a ton of real-life applications. Its popularity resides around most of the FANG companies and more. In this article, we'll cover the basic concepts of Python, as well as look at various real-life problems we could solve using Python! Also, here we will be using Python 3 since Python 2 is almost outdated. So without further adieu let's get started!

image.png

Installation

I'm assuming you all already have Python installed in your system and you are sitting tight with your favorite IDE, if so skip this section and start from Hello World. If not, fret not, I've got you :)

  1. Open up your browser.
  2. Visit python.org.
  3. Click on Downloads.
  4. Download the Latest Version.
  5. Or scroll down a bit to download the version of your choice.
  6. After downloading, click on the executable file.
  7. Check the "Add Python 3.x to PATH" checkbox.
  8. Click on next and follow the steps with default settings.
  9. After Installation click on the close button.
  10. Open your start menu and type in Python and click on IDLE to run your default IDE (Integrated Development Environment) that comes with Python. (You can also use other supercool IDE's out there, Click here to know more! )

image.png

image.png

image.png

image.png

image.png


Hello World

Let's kick things off by creating a simple program that displays the text "Hello world!". In Python, we use the print function to output text. So to generate our message, the code would look like this:

print('Hello world!')

You've just written your first Python program. Easy, right? We will talk more about why Hello World is enclosed in single quotes when we get into Datatypes, Hang tight until we get there!

What else can we do with the print statement? Well for starters, if you want to output multiple lines of text, you can probably use multiple print statements in multiple lines! It goes like this:

print(' Hello! This is Line 1. ')

print(' Hello! This is Line 2. ')

print(' Hello! This is Line 3. ')

What else? OH YEAH! Let's talk about calculations. Doing a calculation in Python is soooooo simple, just enter it into the print statement and that's pretty much it!

print(10–5)
print(1+2+3+4+5)

Multiplication and division in Python are also pretty simple. We use an asterisk * to multiply and a forward slash / to divide. Again, we just enter it straight into the print statement:

print(10*5)
print(10/5)

Note: Using a single slash to divide numbers produces a decimal (or float, as it's known in programming). Eg: print((4 + 8) / 2) would output 6.0 as the result and not 6 !

Just like in regular math, we can use parentheses to indicate the operations we want to be performed first:

print(105(5+5))

Alright enough of print statements, let's know a little bit about the data types used in Python. Are you ready?

Data Types


image.png

Before we go any further, it's a good idea to introduce the main types of data that we use in Python. Each value in Python has a type.

Text, like "Hello world", is called a string.

Whole numbers are called integers.

And numbers with a decimal point are called floats.

Now, we've already encountered strings and integers, so it's time for us to FLOAT! So we know what floats are, they're numbers with a decimal, but how do we produce them? Well, we can produce a Float by dividing any two integers. Or we can also run an operation on two floats, or on a float and an integer. Like this:

print( 8 / 2 )
print( 6 * 7.0 )
print( 4 + 1.65 )

A float can be added to an integer because Python automatically converts the Integer to a Float. Yep Python is smart! Alright, that covers the very basic operations, addition, subtraction, multiplication, and division. You're doing great if you have come this far in this article! It's time to kick it up a notch and introduce exponentiation–which is what we call it when we raise one number to the power of another. The exponentiation operation is performed using two asterisks:

print(5**3), print's 5 to the power of 3.

You can chain exponentiations together. In other words, you can raise a number to multiple powers. For example, 232. You can also try exponentiation with floats, the result would also be a float:

print(9**(1/2)), print's the square root of 9.*

Another interesting stuff in Python is finding the Quotient and Remainder. We use Floor Division to find the quotient and Modulus to find the remainder. Floor division is done using two forward slashes, and Floor division is just like a normal division operation except that it returns the largest possible integer. This integer is either less than or equal to the normal division result:

print(10//4), print's 2.

When it comes to Remainder, we use the modulo operator-which is carried out with a percent symbol (%)–to get the remainder of a given division:

print(7%(5//2)), print's 1.

Given below is a little real-life problem you could try to solve using python, let me know in the comments if you need any help.
image.png

Strings

We actually started off with Strings in this article, print('Hello World'), remember yet? If you were wondering about the single quotes, I've got something for you. We can create a string by entering text between two single or double quotation marks. Like this:

print('Hello World')
print("Hello World")

But what if you want to include a single quote in a single-quoted string (or a double quote in a double-quoted string)? Normally this would confuse Python and break the code, that is unless you escape the quotes using a backslash. Backslashes that we use for escaping, don't get displayed in the output:

print('I\'m learning!')

Okay, so previously as we discussed, we tried to print texts in different lines by using multiple print statements. But is it really necessary? Nope, we've got other ways to do that! To create a new line we use \n. \n can be used in strings to create multi-line outputs. Like this:

print('One \nTwo \nThree')

Similarly, we have various other escape characters as well, like \t for tab space, and so on. Click here, to know more!
Although \n is useful, it can be a bit of a pain if we're trying to format lots of multiline text. Oh yes, There's another way too! Newlines are automatically added for strings created using three quotes (double or single). This makes it easier to format long, multi-line texts without the need to explicitly put \n for line breaks, like:

print(''' Hi this is great''') print(""" Hi this is great too""")

In Python math works with words as well as numbers. So not only can we add integers and floats, but also strings, using something called concatenation, which can be done on any two strings. Like this:

print("Ritchie"+"Pulikottil")

And it even works with numbers! Strings containing numbers are still added as strings rather than integers. Like this:

print("2"+"2")

But don't try to add a string to a number! Even though they might look similar, they are two different entities, so doing this will break the code and produce an error.

You may not be able to add strings to integers, but you can multiply by them! Multiplying a string by an integer produces a repeated version of the original string. Like this:

print("Ritchie" 3)* produces RitchieRitchieRitchie

print(4 '2')* produces 2222

But don't try to multiply a string by another string. This will just generate an error. The same will happen if you try to multiply a string by a Float, even if the Float is a number.

Variables

image.png

A variable lets you store a value by assigning it to a name. The name can be used to refer to the value later in the program. For example, In game development, you would use a variable to store the points of the player. What's the point of playing if you don't know who's winning right! To assign a variable, use one equals sign:

name= "Ritchie", here the string Ritchie is attached to the variable, name.

Naming your variables is pretty flexible. You can use letters, numbers, and underscores in variable names. But you can't use special symbols or start the name with a number. Also, Python is a case-sensitive language. This means Lastname and lastname are two different variable names.

Input

What function would we use to get input from the user in Python? Drumroll, please! That's right, the input function! Python likes to keep things simple and intuitive. So a game can ask for the user's name and age as input and then use them in the game. The input function prompts the user for input and returns what they enter as a string. Like this:

x = input("Enter your name: ")
y = input()
print(x)
print(y)

Right, so we know that the input() function returns a string. But what if we need it to be something else, like a number? No problem, we can totally do that! Let's assume we've taken the age of the user as input. To convert it to a number, we can use the int() function:

age=int(input("Enter your age:")
print(age)

But wait, there's more! We can do the same thing in reverse, and convert a number to a string, but this time we need to use the str() function. This can be useful if you need to use a number in string concatenation.

age = 42
print("His age is " + str(age))

image.png

And with that, we have had a glance at how to get started with Python Programming, up next, we are gonna look at the control flow, loops, functions, and much more, until then take care :)

GitHub
LinkedIn
Twitter
Instagram