Built-In Functions
In my first post, I emphasized that Python is a versatile, high-level programming language. This is because Python provides a wide range of built-in functions available from the start.
As of Python 3.10, there are 69 built-in functions that are readily available for use, meaning you can utilize them without the need for importing or additional configuration. You can view them listed alphabetically in this image from the Python documentation, Built-In Functions.
When in doubt, ask for help()
When you enter the Python shell (via terminal using either python or python3), Python will prompt you to type: “help”, “copyright”, “credits” or “license” for more information if needed.
If you type, help(), you’ll be able to access Python’s help utility! First time users can check out the tutorial at: https://docs.python.org/3.12/tutorial/
🤫 Click q to get out of that help utility
Continuing with help, if you type, help(‘keywords’), you’ll see that, as of Python 3.10, there are 36 keywords. These reserved words have special meaning in Python and cannot be used as identifiers (like variable names).
But if you’ve reached this far in the post and are feeling adventurous, type: help(‘str’), or honestly any built-in function word. This will provide detailed information about that function, including what it does and how you can use it.
None of these built-in functions or keywords are immediately obvious. While you can choose to memorize them, it's always helpful to read about them. And remember, help() is always available to assist you!
Variables
Variables could be a term that we all might recognize algebra, calculus, or any math-related subject really. That’s because we use variables in python the same way we use variables in math: Variables symbolize and refer to some sort of data.
Variables can be short or descriptive, but you'll quickly find that using single letters (x, y, z) for every variable can become confusing, especially when you're dealing with a thousand lines of code.
And python has rules on how to name variables.
DO
Do start with a letter or the underscore character
Do name a variable with only alpha-numeric characters and underscores (A-z, 0-9, and _ )
DONT
Do NOT start with a number
Do NOT use dashes (-)
REMEMBER: variables are case-sensitive so firstname, Firstname, FirstName and FIRSTNAME would be considered different variables.
Fun fact: This type of naming is similar to archiving, especially with digital preservation! Just as you need a clear naming convention for your assets to ensure they make sense to you and your organization while following digital preservation guidelines, you're essentially following Python's variable naming rules.
Declaring Multiple Variables (in a line)
You can declare multiple variables in one line so you can take up less space, which is nice:
coding_language, language, country, year, challenge_started = 'Python', 'English', 'USA', 2024, TrueBUT it has to be in the same order. Try copying and pasting this line to print out the variables—you'll see it's all mixed up, right? This shows that order is crucial when declaring multiple variables on the same line.
coding_language, language, country, year, challenge_started = True, 2024,'Python','English', 'USA'
print("Coding Language: ", coding_language)
print("Language: ", language)
print("Country: ", country)
print("Year: ", year)
print("Challenge Started? ", challenge_started)Ask the user
Hard coding variables isn’t really dynamic. There is a built-in function you can use to get the user input and have the variable be set according to their answer! It has an intuitive name so you can remember it: input()
first_name = input('What is your name: ')
print("Hello", first_name)Data Types
There are several data types in Python. To identify the data type we use the type built-in function. I would like to ask you to focus on understanding different data types very well. When it comes to programming, it is all about data types. I introduced data types at the very beginning and it comes again, because every topic is related to data types. We will cover data types in more detail in their respective sections.
Checking Data types
I previously mentioned only a few of the more commonly used ones, but Python has many built-in data types, each serving a distinct purpose.
Numeric Types: int, float, complex
Sequence Types: str, list, tuple
Mapping Types: dict
Set Types: set, frozenset
Boolean Types: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
Other: range, enumerate, type
To determine the type of an object, you can use type(), which represents the object's type (on a meta-level).
Examples:
print(type('Python'))
print(type(10))
print(type(3.14))
print(type(1 + 1j))
print(type(True))
print(type([1, 2, 3, 4]))
print(type({'language':'Python','challenge_days': 2, 'challenge_started':True}))
print(type((1,2)))
print(type(zip([1,2],[3,4])))
Casting
Casting is the process of converting one data type to another. For example, functions like int(), float(), str(), list(), and set() are used for this purpose. When performing arithmetic operations, string numbers must first be converted to int or float. Similarly, when concatenating a number with a string, the number should be converted to a string first. Otherwise, an error will occur and Python will yell at you 😩
Exercises Day 2
I've detailed the technical aspects of my commits and exercises so make sure to follow along on my GitHub
Level 1
Inside 30DaysOfPython create a folder called day_2. Inside this folder create a file named variables.py
Write a python comment saying 'Day 2: 30 Days of python programming'
Declare a first name variable and assign a value to it
Declare a last name variable and assign a value to it
Declare a full name variable and assign a value to it
Declare a country variable and assign a value to it
Declare a city variable and assign a value to it
Declare an age variable and assign a value to it
Declare a year variable and assign a value to it
Declare a variable is_married and assign a value to it
Declare a variable is_true and assign a value to it
Declare a variable is_light_on and assign a value to it
Declare multiple variable on one line
Level 2
Check the data type of all your variables using type() built-in function
Using the len() built-in function, find the length of your first name
Compare the length of your first name and your last name
Declare 5 as num_one and 4 as num_two
Add num_one and num_two and assign the value to a variable total
Subtract num_two from num_one and assign the value to a variable diff
Multiply num_two and num_one and assign the value to a variable product
Divide num_one by num_two and assign the value to a variable division
Use modulus division to find num_two divided by num_one and assign the value to a variable remainder
Calculate num_one to the power of num_two and assign the value to a variable exp
Find floor division of num_one by num_two and assign the value to a variable floor_division
The radius of a circle is 30 meters.
Calculate the area of a circle and assign the value to a variable name of area_of_circle
Calculate the circumference of a circle and assign the value to a variable name of circum_of_circle
Take radius as user input and calculate the area.
Use the built-in input function to get first name, last name, country and age from a user and store the value to their corresponding variable names
Run help('keywords') in Python shell or in your file to check for the Python reserved words or keywords





