Python variables

| | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

#! / usr / bin / python

 
# Integer assignment

age = 45  

 
# Floating point

salary = 1456.8  

  
# String

name = "John"  

 

print (age)

print (salary)

print (name)

Exit:

 45 1456.8 Jo hn 

The rules for creating variables in Python are the same as in other high-level languages. They are:

a) Variable names must start with a letter or an underscore. 
b) Variable names cannot start with a digit. 
c) Variable names can only contain alphanumeric and underscore characters (Az, 0-9 and _). 
d) Variable names are case sensitive (name, Name and NAME — are three different variables). 
e) Reserved words (keywords) cannot be used with variable names.

Assigning one value to multiple variables:
Python also allows one value to be assigned to multiple variables at the same time. 
For example:

#! / usr / bin / python

 

a = b = c = 10  

 

print (a)

print (b)

print (c)

Exit:

 10 10 10 

Assigning different values ​​to multiple variables:

#! / usr / bin / python

 

a, b, c = 1 , 20.2 , "GeeksforGeeks"  

 

print (a)

print (b)

print (c)

Exit:

 1 20.2 Python.Engineering 

Can we use the same name for different types?
If we use the same name, the variable starts to refer to a new one value and type.

#! / usr / bin / python

 

a = 10

a = "GeeksforGeeks"  

 

print (a)

Exit :

 Python.Engineering 

How does the + operator work with variables?

#! / usr / bin / python

 

a = 10

b = 20  

print (a + b)

 

a = "Geeksfor"

b = "Geeks"

print (a + b)

Exit:

 30 Python.Engineering 

Can we use + for different types?
Failure to use for different types will result in a mistake ibke.

#! / usr / bin / python

 

a = 10

b = "Geeks"

print (a + b)

Output:

 TypeError: unsupported operand type (s) for +: `int` and` str` 

Creating objects (or class type variables):
Please refer to class, object and members for more details.

Exit :

 cse cse 101 cse 

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python variables, check other code Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Chen Emmerson

Moscow | 2023-01-27

Maybe there are another answers? What Python variables exactly means?. I am just not quite sure it is the best method

Oliver Richtgofen

Berlin | 2023-01-27

time Python module is always a bit confusing 😭 Python variables is not the only problem I encountered. Will get back tomorrow with feedback

Jan Chamberlet

Paris | 2023-01-27

Maybe there are another answers? What Python variables exactly means?. I am just not quite sure it is the best method

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically

# Python program to show that variables with the value # assigned in the class declaration, are class variables and
# variables inside methods and constructors are instances
# variables.

 
# Computer Science Student Class

class CSStudent:

 

# Class variable

stream = ` cse`  

 

# init method or constructor

def __ init __ ( self , roll):

  

# Instance variable

self . roll = roll 

 
# CSStudent class objects

a = CSStudent ( 101 )

b = CSStudent ( 102 )

 

print (a.stream)  # prints & quot; cse & quot;

print (b.stream)  # prints & quot; cse & quot;

print (a.roll)  # prints 101

 
# Class variables can be available using the class
# name also

print (CSStudent.stream) # prints & quot; cse & quot;