Python DataTypes and Varibales

  • Variables : Variables are used to store data values.
  • Data Type: A data type defines the type of value a variable can hold.

Data Types

Python supports several built-in data types:

  • Integer: Whole numbers. Numbers without decimal, e.g., 42
  • Float: Numbers with a decimal point, e.g., 3.14
  • String: Sequence of characters, e.g., "Hello, World!"
  • List: Ordered collection of items, e.g., [1, 2, 3]
  • Dictionary: Collection of key-value pairs, e.g., {"name": "Alice", "age": 30}
  • Tuple: Immutable ordered collection, e.g., (1, 2, 3)
  • Boolean: Represents True or False values, e.g., True
  • NoneType: Represents the absence of a value, e.g., None

Syntax for defining variables and datatypes

# Examples of variable assignment
name = "Alice"
age = 30
pi_value = 3.14
is_student = True
        

In above example:

  • name: The variable name is of type string because its value is "Alice", which is a sequence of characters. Any value enclosed in double quotes ("") is considered a string in Python.
  • age: age is varibale of type int because value of variable is number without decimal
  • pi_value: pi_value is varibale of type float because value of variable is number with decimal
  • is_student: is_student is varibale of type bool because value of variable is True. Any varibale with value True of False considered as bool

Python Variable Naming Rules

1. Combination of Upper and Lower Case Characters

Variable names can be a mix of uppercase and lowercase letters.

abcName = ""  # Valid
abc = ""      # Valid
ABC = ""      # Valid

2. Cannot Start with a Number

Variable names cannot begin with a digit, but digits can be used elsewhere in the name.

123abc = ""   # Invalid (SyntaxError)
abc123 = ""   # Valid
anc123Name = ""  # Valid

3. Only Special Character Allowed is Underscore (_)

Variable names can include underscores, but no other special characters are allowed.

_abc = ""     # Valid
abc_ = ""     # Valid
abc_123 = ""  # Valid
abc@123 = ""  # Invalid (SyntaxError)
abc-name = "" # Invalid (SyntaxError)

Notes:

  • Variable names are case-sensitive: abc and ABC are different.
  • Variable names cannot be a Python keyword (e.g., class, def, for).
  • Use meaningful variable names for better code readability.

Python program to declare variables, print their values, types, and sizes

# Variable Declaration Syntax: varibale_name = value
int_number = 10
float_number=14.56
str_name="test"
bln_val=True

# print only variables
print(int_number)
print(float_number)
print(str_name)
print(bln_val)

#print variable with message
print("value of int_number variable:",int_number)
print("value of float_number variable:",float_number)
print("value of str_name variable:",str_name)
print("value of bln_val variable:",bln_val)

# print type of variable
print("type int_number variable:",type(int_number))
print("type float_number variable:",type(float_number))
print("type str_name variable:",type(str_name))
print("type bln_val variable:",type(bln_val))


import sys
# print size of variable
# all size is in byte
print("size of int_number variable:",sys.getsizeof(int_number),"byte")
print("size of float_number variable:",sys.getsizeof(float_number),"byte")
print("size of str_name variable:",sys.getsizeof(str_name),"byte")
print("size of bln_val variable:",sys.getsizeof(bln_val),"byte")

Output:

10
14.56
test
True
value of int_number variable: 10
value of float_number variable: 14.56
value of str_name variable: test
value of bln_val variable: True
type int_number variable: <class 'int'>
type float_number variable: <class 'float'>
type str_name variable: <class 'str'>
type bln_val variable: <class 'bool'>
size of int_number variable: 28 byte
size of float_number variable: 24 byte
size of str_name variable: 53 byte
size of bln_val variable: 28 byte

Understanding User Input and Output

Python allows interaction with users through input and output functions. Below are the key concepts:

  • Input: Using the input() function, you can take input from the user.
  • Output: Using the print() function, you can display data on the screen.
  • Formatted Output: Python allows formatted output using f-strings or the format() method.

Program and Explanation

name = input("please enter your name: ")
print("You entered: ", name)

age = int(input("please enter your age: "))
print("You entered: ", age)

percent = float(input("please enter your percentage: "))
print("You entered: ", percent)

bln = int(input("please enter 0 or 1: "))
isChecked = bool(bln)
print("You entered: ", isChecked)
  

Sample Output

please enter your name: John
You entered:  John

please enter your age: 25
You entered:  25

please enter your percentage: 89.5
You entered:  89.5

please enter 0 or 1: 1
You entered:  True
  

Explanation

The program demonstrates:

  • Taking Input:
    • input(): Captures user input as a string.
    • int(): Converts string input to an integer.
    • float(): Converts string input to a floating-point number.
    • bool(): Converts integer input (0 or 1) to a boolean.
  • Displaying Output:
    • print() is used to display text and variables on the console.

Formatted Output

You can format your output for better readability:

# Using f-strings
print(f"You entered name: {name}, age: {age}, percentage: {percent}, checked: {isChecked}")

# Using format() method
print("You entered name: {}, age: {}, percentage: {}, checked: {}".format(name, age, percent, isChecked))

The formatted output combines text and variable values, making the output more user-friendly.

What Are People Saying About Us

Read These Life Changing Stories From People Just Like You!