0% found this document useful (0 votes)
8 views6 pages

Python Variables and Data Types Guide

The document provides an overview of Python programming, focusing on variables, data types, typecasting, and operators. It explains how to create and use variables, the importance of naming conventions, and the built-in data types such as integers, floats, strings, and booleans. Additionally, it covers type conversion methods and various operators used for arithmetic, comparison, and logical operations.

Uploaded by

Milap Joshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Python Variables and Data Types Guide

The document provides an overview of Python programming, focusing on variables, data types, typecasting, and operators. It explains how to create and use variables, the importance of naming conventions, and the built-in data types such as integers, floats, strings, and booleans. Additionally, it covers type conversion methods and various operators used for arithmetic, comparison, and logical operations.

Uploaded by

Milap Joshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python is a high-level, interpreted programming language known for its

simple and readable syntax. Python was created by Guido van Rossum
and first released in 1991. Its design philosophy emphasizes code
readability and a syntax that allows programmers to express concepts in
fewer lines of code.

A variable is a container for storing data values

# A variable storing a student's name


student_name = "Amit"
# A variable storing a student's age
student_age = 15
# A variable storing a student's score
student_score = 95.5

Rules-
A variable name must start with a letter or the underscore character ( _ ).
A variable name cannot start with a number.
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _).
Variable names are case-sensitive ( age , Age , and AGE are three different
variables).

Convention
Use descriptive names that indicate what the variable is used for (e.g.,
student_name instead of sn ).
Use snake_case for variable names with multiple words (e.g.,
student_first_name ).

Multiple Assignment

x = y = z = 10
print(x) # Output: 10
print(y) # Output: 10
print(z) # Output: 10

# Assigning different values to multiple variables


a, b, c = 5, 10, 15
print(a) # Output: 5
print(b) # Output: 10
print(c) # Output: 15

A data type is a classification that specifies which type of value a variable


has and what type of mathematical, relational, or logical operations can
be applied to it without causing an error.

Built-in data types


Integer ( int ) : An integer is a whole number, without any decimal points.
It can be positive, negative, or zero.
Examples: 10 , -5 , 0 , 12345

Float ( float ) : A float, or a "floating-point number," is a number that has a


decimal point. Floats are used to represent numbers that have a fractional
part.
Examples: 3.14 , -0.5 , 99.99 , 1.0

String ( str ) : A string is a sequence of characters. It is used to store text.


In Python, we create strings by enclosing characters in either single
quotes ( '...' ) or double quotes ( "..." ).
Examples: 'Hello, World!' , "Python is fun" , '12345'

Boolean ( bool ): A boolean is a data type that can have one of two values:
True or False . Booleans are used to represent the truth values of
expressions.

Examples: True , False

Checking Data Types with type()


student_name = "Amit"
student_age = 16
student_height = 5.8
is_present = True
print(type(student_name))
print(type(student_age))
print(type(student_height))
print(type(is_present))

Sometimes, you'll need to convert a value from one data type to another.
This process is called typecasting or type conversion. Typecasting is the
process of converting a variable from one data type to another.

Why Do We Need Typecasting?


Imagine you ask a user to enter their age. When you get input from a user
in Python using the input() function, it is always received as a string. If you
want to perform mathematical calculations with their age (e.g., calculate
their age in 5 years), you first need to convert the string to an integer.

# Get user's age as input


user_age_str = input("Enter your age: ")
# Try to add 5 to the age
# This will cause an error!
# new_age = user_age_str + 5
# Convert the string to an integer first
user_age_int = int(user_age_str)
# Now, we can perform calculations
new_age = user_age_int + 5
print("In 5 years, you will be", new_age, "years old.")

Converting Between Data Types


Python provides simple, built-in functions to convert between data types.

int()
Converts a value to an integer. You can convert floats and strings (that
represent whole numbers) to integers.

float_num = 10.9
str_num = "25"
int_from_float = int(float_num) # Truncates the decimal part
int_from_str = int(str_num)

print(int_from_float) # Output: 10
print(int_from_str) # Output: 25

Common Mistake: You cannot convert a string with non-numeric


characters (except for a leading - ) or a float-like string (e.g., '10.5' )
directly to an integer. This will raise a ValueError .

float()
Converts a value to a float. You can convert integers and strings (that
represent numbers) to floats.

int_num = 15
str_num = "3.14"

float_from_int = float(int_num)
float_from_str = float(str_num)

print(float_from_int) # Output: 15.0


print(float_from_str) # Output: 3.14

str()
Converts a value to a string. You can convert any data type to a string.
This is useful when you want to combine text with numbers in a single
output.

my_age = 15
my_score = 98.5

age_str = str(my_age)
score_str = str(my_score)

message = "My age is " + age_str + " and my score is " + score_str
print(message) # Output: My age is 15 and my score is 98.5

num1_str = input("Enter the first number: ")


num2_str = input("Enter the second number: ")

num1 = float(num1_str)
num2 = float(num2_str)

sum_of_numbers = num1 + num2

Operators

Imagine you are a chef. You have your ingredients (data), but you need
tools to combine and transform them into a delicious dish. In Python,
operators are these tools. They are special symbols that perform
operations on variables and values.

An operator is a symbol that tells the interpreter to perform a specific


mathematical, relational, or logical operation.

Arithmetic Operators
These operators are used to perform mathematical calculations.

price_per_item = 50
quantity = 4
total_cost = price_per_item * quantity
print("Total cost:", total_cost) # Output: Total cost: 200

Comparison Operators

These operators are used to compare two values. They return a boolean
value ( True or False ).

passing_marks = 40
student_score = 55

has_passed = student_score >= passing_marks


print("Has the student passed?", has_passed) # Output: Has the student
passed? True

Logical Operators

These operators are used to combine conditional statements.


is_student = True
age = 15
is_eligible_for_discount = is_student and (age < 18)

print("Eligible for discount?", is_eligible_for_discount)


# Output: Eligible for discount? True

Assignment Operators
These operators are used to assign values to variables.

score = 100
score += 50 # The player earned 50 more points

print("Updated score:", score) # Output: Updated score: 150

You might also like