PYTHON-FUNDAMENTALS: CONSTANTS, VARIABLES AND DATA TYPES

WBOY
Release: 2024-07-27 00:00:23
Original
822 people have browsed it

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
Copy after login

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)
Copy after login

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
Copy after login

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

>>>type("hello")

>>><class'str'>
Copy after login

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
Copy after login

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
Copy after login

*Float (float): Decimal numbers.

>>>value=23.5
Copy after login

*Complex (complex): Complex numbers.

>>>value=2+3j
Copy after login

2. Text Type

String (str): Sequence of characters.
eg:

>>>message="hello mac"
Copy after login

3. Boolean Type

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

>>>my_project=True
Copy after login

4. None Type

NoneType: Represents the absence of a value
eg:

>>>result=none
Copy after login

5. Sequence Types

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

>>>fruits=[apple,cherry,mango]
Copy after login

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

>>>coordinates(3,4)
Copy after login

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

>>>number=range(1,10)
Copy after login

6. Mapping Type

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

>>>person={"name":"kavin","url":"https://www.kavin.com"}
Copy after login

7.Set Type

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

>>>unique_number={2,3,4}
Copy after login

Frozenset (frozenset): Immutable set.
eg:

>>>frozen_set=frozena([2,3,4])
Copy after login

Checking Data Type

Syntax: type(variable_name)
eg:

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

>>> <class'int'>
Copy after login

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

The above is the detailed content of PYTHON-FUNDAMENTALS: CONSTANTS, VARIABLES AND DATA TYPES. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!