DEV Community

nadirbasalamah
nadirbasalamah

Posted on

Python Tutorial - 1 Introduction

Python is one of the most popular programming languages right now, especially with the rise of AI technology. Python is a multi-purpose programming language for developing many things like web applications, back-end services, and data science and machine learning.

Setup

These are the preparations for coding with Python:

  1. Download the Python then install it.
  2. You can use any text editor for writing Python code like Visual Studio Code or a dedicated IDE like PyCharm.

Writing the First Code

Create a new file with a .py extension called main.py. Then write this code.

print("Hello World!")
Enter fullscreen mode Exit fullscreen mode

Run this command to execute Python code.

python main.py
Enter fullscreen mode Exit fullscreen mode

This is the output.

Hello World!
Enter fullscreen mode Exit fullscreen mode

Based on the code above, the print() function displays the Hello World! text.

Variable and Data Types

A variable is a place for storing values like integers, floating numbers, and strings (a bunch of alphanumeric characters). This is an example of variable usage in Python.

number = 42
username = "John Doe"
price = 2.95
Enter fullscreen mode Exit fullscreen mode

To display the value from a variable, use print() function.

number = 42
username = "John Doe"
price = 2.95

# display a value from variable
print("this is a number", number)
print("price: ", price)
# using formatting
print(f"hello, my username is {username}")
Enter fullscreen mode Exit fullscreen mode

This is the output.

this is a number 42
price:  2.95
hello, my username is John Doe
Enter fullscreen mode Exit fullscreen mode

This is the list of commonly used data types in Python.

Data Type Value
Integer non-decimal number
Float decimal number
String Alphanumeric characters
Boolean True or False

Operator

There are many basic arithmetic operators in Python. These operators can be used to perform calculations for number data types like integer and float.

Operator Description
+ add operation
- substract operation
* multiply operation
/ division operation
// floor division operation
% modulo operation (get the remainder from division operation)
** Perform the operation of raising a number to the power of a number

This is an example of operator usage in Python.

first = 4
second = 2

addition = first + second
subtraction = first - second
multiplication = first * second
division = first / second
mod = first % second
square_of_first = first ** 2

print(f'{first} + {second} = {addition}')
print(f'{first} - {second} = {subtraction}')
print(f'{first} * {second} = {multiplication}')
print(f'{first} / {second} = {division}')
print(f'{first} % {second} = {mod}')
print(f'{first} ** {2} = {square_of_first}')
Enter fullscreen mode Exit fullscreen mode

Output

4 + 2 = 6
4 - 2 = 2
4 * 2 = 8
4 / 2 = 2.0
4 % 2 = 0
4 ** 2 = 16
Enter fullscreen mode Exit fullscreen mode

The // operator performs division and then returns the floor of a division result.

result = 29 // 5 # returns 5 (actual value before floor operation: 5.8)
Enter fullscreen mode Exit fullscreen mode

Adding User Input

The input() function reads the input from the user. This function is useful for creating interactive programs in Python. By default, the input() returns String data type.

This is the basic example of using the input() function.

# get username from input
username = input("enter username: ")
# get age from input
# the int() function converts string into integer data type
age = int(input("enter age: "))

print(f"username: {username}")
print(f"age: {age}")
Enter fullscreen mode Exit fullscreen mode

Output

Basic IO in Python

Example 1 - Rectangle Area Calculation

Let's create a rectangle area calculation program in Python. The program allows the user to enter the length and width of the rectangle. Then, the program calculates the area of the rectangle and then displays it to the user.

# get length from user input
length = int(input("enter length: "))

# get width from user input
width = int(input("enter width: "))

# calculate the area of rectangle
area = length * width

# display the result
print(f"area of rectangle: {area}")
Enter fullscreen mode Exit fullscreen mode

Output

Python Program to Calculate Rectangle Area

Example 2 - Get Discounted Price

Let's create a program to calculate the price of an item after the discount is applied. The program allows the user to enter the actual price and discount. Then, the program returns the discounted price.

# get price from user input
price = int(input("enter price: "))

# get discount from user input
discount = int(input("enter discount: "))

# calculate the discounted price
discounted_price = price - (price * (discount / 100))

# display the result
print(f"original price: {price}")
print(f"discounted price: {discounted_price}")
Enter fullscreen mode Exit fullscreen mode

Output

Python Program to Get Discounted Price

Sources

I hope this article helps you learn Python. If you have any feedback, please let me know in the comment section.

Top comments (1)

Collapse
 
msc2020 profile image
msc2020

The gifs help!