Python
  • Getting Started
    • Input and Output
    • Variables
    • Data Types
    • Conditional Structures
      • Humidity Level
      • Financial Transactions
      • Fruit-Vending Machine
      • Magic While
      • FizzBuzz
    • Functions
      • Hashtag Generator
  • Data Structures
    • Lists
    • Dictionary
    • Set
    • Interpreter Expressions
  • OOPs
    • Classes and Objects
    • Exception Handling
      • Simple Calculator
  • System Programming
    • File Operations
    • Directory Navigation
    • Process Creation
    • Threads
Powered by GitBook
On this page
  • Assignment Operator =
  • More on Assignments
  • Working with Variables
  1. Getting Started

Variables

I LOVE DOING THIS :)

Assignment Operator =

>>> speed_of_light = 299792458
>>> print(speed_of_light)
299792458

Do not start with a number, You cannot start a variable name with a number. The rest of the variable name can contain a number

More on Assignments

>>> fav_writers = ["Mark Twain", "Fyodor Dostoyevsky"]
>>> print(fav_writers)
['Mark Twain', 'Fyodor Dostoyevsky']

Another example where you can assign dicts, shown by {...}, to a variable birthdays

>>> birthdays = {"mom": "9Jan", "daughter": "24Dec"}
>>> print(birthdays)
{'mom': '9Jan', 'daughter': '24Dec'}

Working with Variables

Variables will support any method the underlying type supports. For example, if an integer value is stored in a variable, then the variable will support integer functions such as addition

>>> var = 2
>>> print(var + 3)
5

You can make a change in a variable and assign it to the same variable. This is done generally when some kind of data type change is done.

For example, you can take a number as input. This will take in the digit as a string. You can then take the string number and convert it to int and assign it to the same number

>>> number = input()
2
>>> type(number)
<class 'str'>
>>> number = int(number)
>>> type(number)
<class 'int'>

We will use a function range(3) which returns three values.

>>> print(range(3))
[0, 1, 2]

Something that returns three values can be unpacked to three variables. This is like saying take whatever is in range(3) and instead of assigning it to a single variable, break it up and assign individual values to the three variables. This is done using a comma between the variables.

>>> id1, id2, id3 = range(3)
>>> print(id1)
0
>>> print(id2)
1
>>> print(id3)
2
PreviousInput and OutputNextData Types

Last updated 2 years ago