Home > Backend Development > Python Tutorial > Python Basics: Variables, Data Types, and Basic Operators

Python Basics: Variables, Data Types, and Basic Operators

DDD
Release: 2024-11-05 09:29:02
Original
670 people have browsed it

Python Basics: Variables, Data Types, and Basic Operators

Starting Simple: Why Variables Matter

In Python, variables are containers with labels that hold whatever data you want—text, numbers, lists, you name it. You don’t need to tell Python what type of data is in there; it just goes with the flow.

Example:

name = "Alice"
age = 25
height = 5.5
Copy after login

Here, name is a string, age is an integer, and height is a float. Now you can store info without getting bogged down in type declarations.


Diving into Data Types

Python data types cover pretty much all your needs:

  • Integer (age = 25): Whole numbers.
  • Float (height = 5.5): Numbers with decimals.
  • String (name = "Alice"): A sequence of characters.
  • Boolean (is_active = True): True or False.
  • Lists, Tuples, Sets, and Dictionaries: Collections of items—like keeping a bunch of things in separate, labeled boxes.

Operators: Simple Calculations, Big Impact

You’ve got your arithmetic operators ( , -, *, /), but Python’s also got some extras:

Examples:

a = 10
b = 3

print(a + b)  # Addition
print(a ** b) # Exponentiation (fancy word for power)
print(a // b) # Floor division (cuts off decimals)
Copy after login

Real-World Example: Checking Access

Here’s a quick profile check: if the age falls between 18 and 60 and the user’s an admin, they get access.

age = 30
is_admin = True

if 18 <= age <= 60 and is_admin:
    print("Access Granted")
else:
    print("Access Denied")
Copy after login

There you go!

As a wise coder once said, "May your bugs be few and your syntax be clean."

The above is the detailed content of Python Basics: Variables, Data Types, and Basic Operators. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template