Logo

dev-resources.site

for different kinds of informations.

PYTHON-FUNDAMENTALS: CONSTANTS, VARIABLES AND DATA TYPES

Published at
7/26/2024
Categories
python
variables
constants
datatypes
Author
kk_python
Author
9 person written this
kk_python
open
PYTHON-FUNDAMENTALS: CONSTANTS, VARIABLES AND DATA TYPES

hi,everyody
I am kavin. I am going to write which I learnt I my class.

Variables

A variable in Python is a symbolic name that references or points to an object. Once a variable is assigned a value, it can be used to refer to that value throughout the program. Variables act as containers for storing data values.

How to name a variables

1.Start with a letter or an underscore.
2.Followed by letters, digits, or underscores.
3.Case-sensitive
4.Don't use Python Keywords

Examples of Valid Variable Names:
my_variable
variable1
_hidden_variable
userName

Assigning Values to Variables

In Python, the assignment operator = is used to assign values to variables. The syntax is straightforward: variable_name = value.
eg:

>>>name="kavin"
>>>print(name)

>>>kavin
Enter fullscreen mode Exit fullscreen mode

Multiple Assignments

Python allows you to assign values to multiple variables in a single line. This can make your code more concise and readable.
eg:

>>>a,b,c=1,2,3
>>>print(a,b,c)
Enter fullscreen mode Exit fullscreen mode

Variable Types

Python is a dynamically typed language, which means you don’t need to declare the type of a variable when assigning a value to it. The type is inferred at runtime based on the assigned value.
eg:

>>>my_variable="10"

>>>my_variable is an integer
Enter fullscreen mode Exit fullscreen mode

You can check the type of a variable using the type() function.
eg:

>>>type("hello")

>>><class'str'>
Enter fullscreen mode Exit fullscreen mode

Constants

In Python, constants are variables whose values are not meant to change. By convention, constants are typically written in all uppercase letters with underscores separating words.
eg:

>>>PI=22/7
Enter fullscreen mode Exit fullscreen mode

Data Types

Data types are the different kinds of values that you can store and work with.

1.Numeric Types
*Integer (int): Whole numbers.

>>>value=23
Enter fullscreen mode Exit fullscreen mode

*Float (float): Decimal numbers.

>>>value=23.5
Enter fullscreen mode Exit fullscreen mode

*Complex (complex): Complex numbers.

>>>value=2+3j
Enter fullscreen mode Exit fullscreen mode

2. Text Type

String (str): Sequence of characters.
eg:

>>>message="hello mac"
Enter fullscreen mode Exit fullscreen mode

3. Boolean Type

Boolean (bool): Represents True or False.
eg:

>>>my_project=True
Enter fullscreen mode Exit fullscreen mode

4. None Type

NoneType: Represents the absence of a value
eg:

>>>result=none
Enter fullscreen mode Exit fullscreen mode

5. Sequence Types

*List (list): Ordered, mutable collection
eg:

>>>fruits=[apple,cherry,mango]
Enter fullscreen mode Exit fullscreen mode

*Tuple (tuple): Ordered, immutable collection.
eg:

>>>coordinates(3,4)
Enter fullscreen mode Exit fullscreen mode

*Range (range): Sequence of numbers.
eg:

>>>number=range(1,10)
Enter fullscreen mode Exit fullscreen mode

6. Mapping Type

Dictionary (dict): Unordered, mutable collection of key-value pairs.
eg:

>>>person={"name":"kavin","url":"https://www.kavin.com"}
Enter fullscreen mode Exit fullscreen mode

7.Set Type

Set (set): Unordered collection of unique elements.
Eg:

>>>unique_number={2,3,4}
Enter fullscreen mode Exit fullscreen mode

Frozenset (frozenset): Immutable set.
eg:

>>>frozen_set=frozena([2,3,4])
Enter fullscreen mode Exit fullscreen mode

Checking Data Type

Syntax: type(variable_name)
eg:

>>>name="kavin"
>>>print(type(name))

>>> <class'int'>
Enter fullscreen mode Exit fullscreen mode

this is the things which i learnt in the class of Variables, Constants and Data Types.
Thank You

datatypes Article's
30 articles in total
Favicon
14. Longest Common Prefix - Using Trie
Favicon
Variables & Data types
Favicon
Handling Data in SQL: Signed vs. Unsigned Types
Favicon
Representação numérica na computação
Favicon
Understanding Floats in Python: Essential Tips and Examples
Favicon
Everything You Need to Know About Python Integers: Tips, Tricks, and Examples
Favicon
Understanding Python Data Types: A Comprehensive Guide
Favicon
Why I Revisited MS SQL Server Basics: A Deep Dive into String Data Types
Favicon
Understanding Data Types in JavaScript
Favicon
Disjoint Unions in C
Favicon
PYTHON-FUNDAMENTALS: CONSTANTS, VARIABLES AND DATA TYPES
Favicon
Understanding Your Data: The Essentials of Exploratory Data Analysis"
Favicon
Data Types of Typescript
Favicon
C# {Data Types except Int}
Favicon
Variables, Constants, Data Types, and Namespaces in C++
Favicon
Data Types in Python
Favicon
JS Data types (Ma'lumot turlari)
Favicon
Data Types
Favicon
C# da ratsional sonlar bilan ishlovchi (float, double, decimal) ma'lumot turlari
Favicon
Big Integer in Java
Favicon
Oracle Data Types: An Overview
Favicon
Understanding Float vs. Double in C and C++
Favicon
Data Types - Python
Favicon
The Art of Series Summation in C: Navigating Data Types, Casting Magic, and the Dance of Incrementation
Favicon
Understanding Why We Don't Use Pointers to change the value of the element in Slice Data Type in Go Lang!
Favicon
A step by step guide to Converting a Column to Date Data Type in a Dataset using R
Favicon
Choosing the Right Java Data Types
Favicon
Evolution of Ruby Data Types
Favicon
Composite Data types part 1
Favicon
Network Address Types in PostgreSQL. Why you need to know?

Featured ones: