- 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 program that prints the type of each variable:
name = "Alice" age = 30 pi_value = 3.14 is_student = True print("Type of name:", type(name)) print("Type of age:", type(age)) print("Type of pi_value:", type(pi_value)) print("Type of is_student:", type(is_student))
Output:
Type of name: <class 'str'>
Type of age: <class 'int'>
Type of pi_value: <class 'float'>
Type of is_student: <class 'bool'>
Using the input()
Function in Python
The input()
function in Python is used to take user input from the keyboard. It pauses the program's execution and waits for the user to type something. When the user presses the "Enter" key, the input is read as a string and returned.
Example Usage:
name = input("Enter your name: ")
print("Hello, " + name + "!")
Explanation:
- The
input("Enter your name: ")
function displays the prompt"Enter your name: "
on the screen and waits for the user to type their name. - The text entered by the user is stored in the
name
variable as a string. print("Hello, " + name + "!")
then prints a greeting message that includes the user's input.
Key Points:
- Default Data Type: The
input()
function always returns the user input as a string. If you need a different data type (like an integer or float), you must convert it usingint()
,float()
, or another conversion function.
Example:
age = int(input("Enter your age: ")) # Converts the input to an integer
pi_value = float(input("Enter the value of pi: ")) # Converts the input to a float
- Prompt Message: The string inside the
input()
function (like"Enter your name: "
) is the prompt message displayed to the user.
Full Example:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
pi_value = float(input("Enter the value of pi: "))
is_student = input("Are you a student? (yes/no): ").lower() == "yes"
print("Name:", name)
print("Age:", age)
print("Value of Pi:", pi_value)
print("Is Student:", is_student)
In this example:
- The program takes user input for the name, age, value of pi, and whether the user is a student.
- It then prints out the values entered by the user, with the correct data types applied to each input.