<jupyter_start><jupyter_text>Py4E Ch 2 From Section 2.2 Variables  VariablesWe've already played with variables in Bash and now we'll look at them in Python. Other than the syntax, there's not a lot different between Bash and Python variables.<jupyter_code>message = 'And now for something completely different'
n = 17
pi = 3.1415926535897931
print(message)
print(n)
print ("Pi is:", pi)<jupyter_output>And now for something completely different
17
Pi is: 3.141592653589793<jupyter_text>Notice no "$" before variable names in either assigning the value or using the variable.So, while in Bash we could do something like:```[magitz@login4 ~]$ pi=3.14[magitz@login4 ~]$ echo "Pi is: $pi"Pi is: 3.14```In Python, that doesn't work since Python doesn't know you mean the variable pi vs the text "pi":<jupyter_code>print("Pi is: pi")<jupyter_output>Pi is: pi<jupyter_text>Variable typesVariable types determine the kind of data that the variable holds. This impacts how the computer manages the memory where the data are stored. Section 2.2 of Py4E looks at this a bit.<jupyter_code>type(message)
type(n)
type(pi)<jupyter_output><empty_output><jupyter_text>Note that the Jupyter output is a little different than in the Py4E text and what you would get on the Python command prompt:```[magitz@login4 ~]$ python3Python 3.6.5 (default, Apr 10 2018, 00:31:24) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> x=5>>> type(x)>>> ```We'll learn more about classes later, but for now, keep in mind that integers, floats and strings are all **classes**.  Implicit vs explicit typingLike Bash, Pyhton uses **implicit typing** of variables. This is also sometimes called "duck typing". That is, if it looks like an integer, it's an integer. Variables are typed based on what they store.The oposite of implicit typing is **explicit typing** where you, as the coder, must declare--when you define the variable--what type of data it will hold. C, C++ and others rely on explicit typing--this tends to be the case for compiled programs as the machine code needs to be compiled to properly store the data before the data are present. This forces coders to be more explicit about what they plan to do with a variable, but can also be limiting when you might want to not be explicit ahead of time.Implicit typing is handy for programmers because you don't need to think about what type of data the variable will hold...except that you kind of do, because different types do different things when you operate on them.<jupyter_code>x="String"
y="text"
print (x+y)
x=5
y=3
print(x+y)<jupyter_output>8<jupyter_text>Here are some common variable types and examples of the data they correspond to:|Type|Example||----|-------||Integer | 42 ||Float (real) | 3.14 ||Boolean | True ||String | Hello ||List | [1,4,"text",5]||Tuple | (42,"Answer") ||Dictionary | {"Name" : "Matt", "Fingers" : 10} | In many cases you can force Python to interpret a variable has having a particular type. For example:<jupyter_code>x=5
y="Test"
print (x+y)
print (str(x)+y)<jupyter_output>5Test<jupyter_text>In the first case, we get an error because Python doesn't know what to do to add an integer and a string. In the second case, we tell Python to treat x as a string and Python knows how to add (concatenate) strings.But you need to be careful and it doesn't always work...<jupyter_code>num=1.0
print("Convert 1.0 to integer:",int(num))

num=1.5
print("Convert 1.5 to integer:",int(num))

num="two"
print("Convert 'two' to integer:",int(num))<jupyter_output>Convert 1.0 to integer: 1
Convert 1.5 to integer: 1<jupyter_text>From Section 2.3 Variable namesThere are many (conflicting) opinions on variable naming. Consistency wins, but the [PEP8 Style Guide](https://www.python.org/dev/peps/pep-0008/) says that variable and function names should be **lower_case_with_underscores** connecting multple words.  From Section 2.10Example of input()<jupyter_code>inp=input()
print(inp)
name=input("What is your name?\n")
print("Hi", name,", nice to meet you!")<jupyter_output>What is your name?
Matt
Hi Matt , nice to meet you!<jupyter_text>Notice the spaces that print puts between the things we told it to print. That can also be controlled (we just need to put spaces where we *do* want them):<jupyter_code>name=input("What is your name?\n")
print("Hi ", name,", nice to meet you!", sep="")
prompt = 'What...is the airspeed velocity of an unladen swallow?\n'
speed = input(prompt)
int(speed)<jupyter_output>What...is the airspeed velocity of an unladen swallow?
17
{'fraction_non_alphanumeric': 0.1020497165285652, 'fraction_numerical': 0.028347143480157, 'mean_word_length': 5.240816326530612, 'pattern_counts': {'":': 1, '<': 26, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '71344', 'license': 'MIT', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1361, 'n_words': 624, 'path': 'JupyterNotebooks/Variables_and_Types.ipynb', 'repo_name': 'CompTools/PS2-lepidopetera', 'size': '4586'}