Python Coding 101 | Introduction and Basics

Basic Syntax, Variables, Type Function, Input from User, Input function with a message, Data Types, Type Conversion, Your First ever Python Program

All my posts are #WrittenByHuman

Last year I had 0 of you.

Today I have:
25,000+ of You from around the world (LinkedIn)
→ Successful Data Science & AI Live Cohort for Leaders/Data Professionals
→ Data Science, Engineering & Architecture Services & Consulting Company

This wouldn't be possible without you.

More is coming your way.

Happy Learning!

Basic Syntax

print("Hello World")

Output:

Hello World

print function will change the line after printing the string provided in double-quotes.

For example:

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

Output:

Hello
World

You can do the same with new line character \n

New Line character

It will bring the cursor to the next line, just like when we press Enter in Notepad.

# \n - new line character

print("Hello\nWorld")

Output:

Hello
World

Horizontal Tab

It will provide a space of 8 characters.

# \t - horizontal tab

print("Hello\tWorld")

Output:

Hello World

Practice Questions

1 → Write a program to print the following output:

Your Name
College Name
City Name

print("Himanshu\n\tDAVV\n\t\tIndore")

2 → Write a program to print the following output:

*
**
***
print("*\n**\n***")

3 → Write a program to print the following output:

    *
*       *
    *
print("    *\n*\t*\n    *")

Variables

Variables are the same as the variables that were used in mathematics. Remember we used to find X? (Not your EX Chill!!)

In Python, variables are used to store values.

a = 10
print(a)

Output:
10

This means a is a variable that holds a value 10

Just like that, simple.

a = 10.5
print(a)

Output:
10.5

This means a is a variable that holds a value 10.5

a = "Sun"
print(a)

Output:
‘Sun’

This means a is a variable that holds a value 'Sun'

Note: Python Supports dynamic data types, that means you do not need to specify the type of a variable, it will detect automatically.

Type function

Type function will return the type of the variable, this is how we can know the data type of a variable.

a = 10

print(a)

print(type(a))

Output:
10
<class ‘int’>

b = 10.5

print(b)

print(type(b))

Output:
10.5
<class ‘float’>

Note: All decimal values in programming are named as float

c = "Indore"

print(c)

print(type(c))

Output:
‘Indore’
<class ‘str’>

Note: str is for string values.

You see the class keyword while printing the type of a variable, this is because all the variables in Python are objects of different classes.
Here class str means the string Indore is the object of str class.

Don’t worry about the above statement as we will understand it in detail in the Object Orientation topic.

Input from User

Have you used an ATM machine? Ever wonder how it is taking input from us of passwords, where that password got stored temporarily?

Well, the input function is the answer to that question. If you want to get input from the user

a = input()

print(a)

When this executes, the input function will hold the program and let the user give the input and the cursor will be blinking. When you enter any value and press enter, the values get stored in the variable a respectively.

For example, if the user enters 10, then the output will be:
10
10

The first one is by the user and the second is by the print statement.

Let’s try it again.

a = input()

print(a + 10)

If the user gives the input 5, the output will be:
5
15

Input function with a message

If you want to print a message in front of the user, you can pass the message inside the function like this:

a = input("Enter Your Password")

print(a)

Output:

Enter Your Password: (The cursor will be blinking here)

When the user enters the value and presses enter, the value is received by the variable a

Output:
Enter Your Password: 1234
1234

Let’s take one more example:

a = input("Enter Your Name")

print("Hello", a)

Output:
Enter Your Name: Himanshu
Hello Himanshu

Note that the right side part of = will always work first, it is processed and assigned to the left side variable, in this case a

Data Types

Type Conversion

It is the process of converting a variable from one type to another.

To convert variables from one type to another there are some direct in-built functions:

int(), float(), str(), bool() and more.

a = 10.5
print(type(a))

b = int(a)   # converted a float value into an integer

print(b)
print(type(b))

Output:
< class ‘float’ >
10
< class ‘int’ >

a = 10.65
print(type(a))

b = str(a)   # converted a float value into string

print(b)
print(type(b))

Output:
< class ‘float’ >
‘10.65’
< class ‘str’ >

Your First-Ever Program Program

Question: Write a program to enter 2 values from the user and print the addition of them.

a = input("Enter 1st number: ")
b = input("Enter 2nd number: ")

c = a + b

print(c)

Output:
Enter 1st number: 10
Enter 2nd number: 20
1020

Note: the output of the program should be 30 but here it is 1020, why? Because the type of the variables a and b is str (when you take values from user in python, the default type will always be string).

When you add 2 strings, they will concatenate, which means it will join.

"10" + "20"   # output will be "1020"
"Hi" + "Bye"  # output will be "HiBye"

So to solve this problem we have to typecast the variables into int

Let’s do that.

a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))

c = a + b

print(c)

Output:
Enter 1st number: 10
Enter 2nd number: 20
30

Let’s do this again, by printing the proper message.

a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))

c = a + b

print("Addition =",c)

Output:
Enter 1st number: 10
Enter 2nd number: 20
Addition = 30

That’s it, You have covered the following topics:

  • Basic Syntax

  • Variables

  • Type Function

  • Input from User

  • Input function with a message

  • Data Types

  • Type Conversion

  • Your First ever Python Program

These are the foundation topics for all the Python Absolute Beginners.

Reply

or to participate.