Home  >  Article  >  Backend Development  >  Python Tutorial - ntroduction

Python Tutorial - ntroduction

王林
王林Original
2024-08-25 06:01:50401browse

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!")

Run this command to execute Python code.

python main.py

This is the output.

Hello World!

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

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}")

This is the output.

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

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}')

Output

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

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)

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}")

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}")

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}")

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:
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