DEV Community

Cover image for Day 3: Data types and variables in python šŸ§”
aryan015
aryan015

Posted on • Updated on

Day 3: Data types and variables in python šŸ§”

complete index
my-linkedinšŸ§”

You do not need to specify which data you assigning to variable. It is smartā›“ enough to recognize which datatype you are holding.
We will understand data type and variables hand to hand.

python variables

Variable in programming(in general) is nothing but a value that can be used multiple times in your code.

syntax

# syntax
# variable_name = data
Enter fullscreen mode Exit fullscreen mode

Look at below bad example

# take the user input name 
# and append welcome as a prefix
# bad apprach that might annoy user
print("welcome "+input("enter your name"))
print("bye "+input("enter your name"))
# use might save variable space (memory space) but it will be bad user experience
Enter fullscreen mode Exit fullscreen mode

Now example (good)

# good approach
# you might need username in future reference for the code so
name = input("enter your name")
print("welcome "+ name)
print("bye ")
# good approach
Enter fullscreen mode Exit fullscreen mode

Variable naming

  • use camleCasing
  • a variable name cannot start with a number. 9ty must be invalid.
  • Only special symbol allowed are underscore (_).

all below variable are valid

user_name = "aryan"
g8 = "aryan"
Enter fullscreen mode Exit fullscreen mode

supported datatypes [important]

I don't want to afraid you with hordes of data category.

# 1. string
# nothing but values between " and '
name = "aryan"
# 2. integer
# a number 
age = 26
PI = 3.14 # float
# 3. bool
# a value which either true or false
isQualify = True
canVote = False
# 4. list/array
# python array can hold multiple values (a container for different data)
fruits = ["apple","BananašŸŒ","mango"]
# 5. Dictionary
# a datatype that holds key value pair
# As of Python 3.7, dictionaries are ordered (items have a defined order).
# In Python 3.6 and earlier, dictionaries are unordered.
dict = {"brand": "Ford", "model": "Mustang", "year": 1964}
# 6. tuple
# an immutable datatype that contains any number of value and datatype and are ordered.
any_tuple = (1, "hello", 3.14, True, [10, 20])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)