Home Backend Development Python Tutorial Python Tutorial - ntroduction

Python Tutorial - ntroduction

Aug 25, 2024 am 06:01 AM

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!")
Copy after login

Run this command to execute Python code.

python main.py
Copy after login

This is the output.

Hello World!
Copy after login

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
Copy after login

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}")
Copy after login

This is the output.

this is a number 42
price:  2.95
hello, my username is John Doe
Copy after login

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}')
Copy after login

Output

4 + 2 = 6
4 - 2 = 2
4 * 2 = 8
4 / 2 = 2.0
4 % 2 = 0
4 ** 2 = 16
Copy after login

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)
Copy after login

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}")
Copy after login

Output

Python Tutorial - ntroduction

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}")
Copy after login

Output

Python Tutorial - ntroduction

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}")
Copy after login

Output

Python Tutorial - ntroduction

Sources

  • Python Official Page.
  • Python Tutorial.

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

The above is the detailed content of Python Tutorial - ntroduction. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles