0% found this document useful (0 votes)
2 views

01-Variable Assignment

The document outlines the rules for variable assignment in Python, including naming conventions and the concept of dynamic typing. It discusses the pros and cons of dynamic typing, how to assign and reassign variables, and how to determine variable types using the type() function. Additionally, it provides a simple exercise to illustrate the use of variables in calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

01-Variable Assignment

The document outlines the rules for variable assignment in Python, including naming conventions and the concept of dynamic typing. It discusses the pros and cons of dynamic typing, how to assign and reassign variables, and how to determine variable types using the type() function. Additionally, it provides a simple exercise to illustrate the use of variables in calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

b6j85tn96

March 25, 2025

___
Content Copyright by Pierian Data

1 Variable Assignment
1.1 Rules for variable names
• names can not start with a number
• names can not contain spaces, use _ intead
• names can not contain any of these symbols:
:'",<>/?|\!@#%^&*~-+
• it’s considered best practice (PEP8) that names are lowercase with underscores
• avoid using Python built-in keywords like list and str
• avoid using the single characters l (lowercase letter el), O (uppercase letter oh) and I (upper-
case letter eye) as they can be confused with 1 and 0

1.2 Dynamic Typing


Python uses dynamic typing, meaning you can reassign variables to different data types. This makes
Python very flexible in assigning data types; it differs from other languages that are statically typed.
[1]: my_dogs = 2

[2]: my_dogs

[2]: 2

[3]: my_dogs = ['Sammy', 'Frankie']

[4]: my_dogs

[4]: ['Sammy', 'Frankie']

1
1.2.1 Pros and Cons of Dynamic Typing
Pros of Dynamic Typing
• very easy to work with
• faster development time

Cons of Dynamic Typing


• may result in unexpected bugs!
• you need to be aware of type()

1.3 Assigning Variables


Variable assignment follows name = object, where a single equals sign = is an assignment operator
[5]: a = 5

[6]: a

[6]: 5

Here we assigned the integer object 5 to the variable name a.Let’s assign a to something else:
[7]: a = 10

[8]: a

[8]: 10

You can now use a in place of the number 10:


[9]: a + a

[9]: 20

1.4 Reassigning Variables


Python lets you reassign variables with a reference to the same object.
[10]: a = a + 10

[11]: a

[11]: 20

There’s actually a shortcut for this. Python lets you add, subtract, multiply and divide numbers
with reassignment using +=, -=, *=, and /=.
[12]: a += 10

2
[13]: a

[13]: 30

[14]: a *= 2

[15]: a

[15]: 60

1.5 Determining variable type with type()


You can check what type of object is assigned to a variable using Python’s built-in type() function.
Common data types include: * int (for integer) * float * str (for string) * list * tuple * dict (for
dictionary) * set * bool (for Boolean True/False)

[16]: type(a)

[16]: int

[17]: a = (1,2)

[18]: type(a)

[18]: tuple

1.6 Simple Exercise


This shows how variables make calculations more readable and easier to follow.
[19]: my_income = 100
tax_rate = 0.1
my_taxes = my_income * tax_rate

[20]: my_taxes

[20]: 10.0

Great! You should now understand the basics of variable assignment and reassignment in
Python.Up next, we’ll learn about strings!

You might also like