Python Basics - Part 1

Learn the basics of python in this blog.

ยท

3 min read

Python Basics - Part 1

Displaying Output

To display output in the terminal we use the print() function.

Syntax: print(text to display)

Example:

print("Hello World")

Taking Input

For taking input, we use the input() function.

Syntax: input(text to display for input)

Example:

name = input("Enter your name: ")
print("Hi, " + name)

Comments

Programmers use comments to explain parts of code in their program.

Single-line comment: For only commenting a single line we use #.

Multiple line comment: For commenting multiple lines we use """ at the start and also end it using """.

Usage:-

#Single line comment
"""
Multiple line
comment
"""

Variables

There are 4 types of variables:-

  1. str: It stores a character or a list of characters. (Use " " during initializing a string value)

  2. int: It stores an integer.

  3. float: It stores a decimal number.

  4. bool: It can store only 2 values, True or False (Remember the t and f in true and false are always capital)

initializing a Variable

To assign a value to a variable we simply use '='.

It is not necessary to define the data type of the variable and can even be changed after it is set.

Syntax: <variable name> = <value>

Example:

a = 9            #variable a storing integer value 9.
b = "Hello"      #variable b storing a string Hello.
c = True         #variable c storing boolean value True.
d = 8.7          #variable d storing float value 8.7 .

Type Casting

Sometimes you might want to change the data type of the variable for specific purposes. This can be done by using type casting.

Syntax: <variable name> = <data type to change into>(<value>)

Example:

#Convert integer to string
a = 6
a = str(6)
print("The number is " + a)
#Output: The number is 6

#Converting float to integer
b = 8.7
b = int(8.7)
print("Value of b is "+b)
#Output: Value of b is 8

Arithmetic Operations (Basic)

You can also perform mathematical operations in python such as addition, subtraction etc!

Operators:-

  1. Addition

  2. Subtraction

  3. Division

  4. Multiplication

  5. Modulus

  6. Floor Division

  7. Exponent

Addition

"+" is the addition operator.

a = 6
b = 7
print(a+b)
#Output: 13

Subtraction

"-" is the subtraction operator.

a = 5
b = 4
print(a-b)
#Output: 1

Division

"/" is the division operator.

a = 15
b = 5
print(a/b)
#Output: 3

Multiplication

"*" is the multiplication operator.

a = 8
b = 7
print(a*b)
#Output: 56

Modulus

"%" is the modulus operator. It is used to store only the remainder in the division.

a = 10
b = 5
print(a%b)
#Output: 0

Floor Division

"//" is the floor division operator. It is used to round of the result of the quotient of the division to the nearest integer.

a = 3
b = 2
print(a//b)
#Output: 1
#Explanation: The division will give the quotient as 1.5, after rounding it off it is going to become 1.

Exponent

"**" is the exponentiation operator. It is used to increase the value of the first variable to the power of the second variable.

a = 1
b = 3
print(a**b)
#Output: 1
"""
Explanation: The value is found by multiplying 1 three times, that is to raise the value of 1 to the power of 3.
1*1*1 = 1
"""

Conclusion

If you have any doubts, feel free to comment it!

Thank you for reading, if you enjoyed the post don't forget to like and subscribe to my newsletter to get updated about my future posts ๐Ÿ˜ƒ

-Coding Storm,

The storm is coming โšก

https://www.instagram.com/cstorm.official/

ย