Professional Documents
Culture Documents
01 Python Variables Types and Basic Io
01 Python Variables Types and Basic Io
1
lrgnn2bq\appdata\roaming\python\python311\site-packages (from
tqdm<5.0.0,>=4.42.1->pyppeteer) (0.4.6)
chromium is already installed.
[ ]: # BTW, in a Python program any sequence after a '#' is a comment (like this␣
↪line)
''' Talking about comments ... multiple line comments can be written within␣
↪triple quotes
as shown here
'''
[ ]: # What is a variable?
# Variables in Python (... or, in any programming language) are 'containers',␣
↪that are used to store data.
[ ]: # Introducing the simplest way to print the value of a variable ... and also␣
↪your introduction to a 'function'!
2
# In the following statements, we are calling, or 'invoking' the 'print'␣
↪function
# More about 'defining' functions in later modules ... till then we just use␣
↪them
# There are also user-defined data types that can be a combination of built-in␣
↪data types ...
# Python is dynamically typed, so you don't need to specify the data type␣
↪explicitly.
[ ]: # Let's create some more variables of the simpler data types, with more␣
↪meaningful identifiers
3
print("Height:", height)
print("Is Student:", is_student)
print("Complex Number:", complex_num)
print("A general variable: its value is", a_general_variable, "and its type is:
↪", type(a_general_variable))
[ ]: # Still better?
print(f" Name: {name}\n Age: {age}\n Height: {height}\n Is Student:␣
↪{is_student}\n Complex Number: {complex_num}")
# For example, specify the total width, justification, and number of␣
↪significant digits of a 'float' number
# Q: Look up the old style output formatting methods also ... old code in␣
↪Python would have them
[ ]: # Notice the use of 'escape' character '\' in the above statement. What is it?␣
↪Why is it needed?
# Q: Which other such escape characters and escape sequences are pre-defined,␣
↪and when are they used?
# Q: Is it possible to fix the type of a variable, ie. once defined the type␣
↪cannot be changed?
4
[ ]: # Conversion functions are available to convert one data type to another.
# Typically, from a string to int, float, etc
# This is required usually while interactively inputting data ... coming up␣
↪later
a = int("1")
b = float("1.1")
c = str(3)
d = bool(5)
print(a,b,c,d)
# If you are using Jupyter Notebook, type 'a.' followed by TAB to get a list of␣
↪functions available for 'a'
i_exist = True
print(i_exist)
[ ]: del(i_exist)
# print(i_exist) # at this point the variable does not exist, hence␣
↪executing this line raises an error
# In the blocks that follow, print out the value of the variable after each␣
↪operation to check the result
# If you are clueless about what something like '+=' means, refer the following␣
↪link
5
# https://docs.python.org/3/reference/simple_stmts.
↪html#augmented-assignment-statements
[ ]: # Assignment operations
a = 10 # Assignment
a += 5 # Addition assignment
a -= 5 # Subtraction assignment
a *= 5 # Multiplication assignment
a /= 5 # Division assignment
a %= 5 # Modulo assignment
a //= 5 # Floor division assignment
a **= 5 # Exponentiation assignment
[ ]: # Arithmetic operations
a = 10 + 5 # Addition
b = 10 - 5 # Subtraction
c = 10 * 5 # Multiplication
d = 10 / 5 # Division
e = 10 // 5 # Floor Division
f = 10 % 5 # Modulo/Remainder
g = 10 ** 5 # Exponentiation
[ ]: # Logical operations
a = True and False # Logical AND
b = True or False # Logical OR
c = not True # Logical NOT
[ ]: # Bitwise operations
a = 10 & 5 # Bitwise AND
b = 10 | 5 # Bitwise OR
c = 10 ^ 5 # Bitwise XOR
d = ~10 # Bitwise NOT
e = 10 << 2 # Left shift
f = 10 >> 2 # Right shift
6
# That is, assign values to variables from an external source (e.g. the user␣
↪using a keyboards)
# using the input() function ... Python display the supplied prompt, and waits␣
↪for a value to be specified
# After you type the following statement, you will find a prompt on the screen,␣
↪somewhere. Don't get lost!
[ ]: # Variable values can be written out into a file using the following code
# Write the variables to a text file
7
[ ]: # That's it in this unit. More types, built-in functions, and more Python␣
↪features in the next unit !