Kursinnhold
Introduction to Python (copy)
Introduction to Python (copy)
Recap
Congratulations on completing this section of our Python course! You've gained a solid foundation in several fundamental programming concepts. Let's review the key skills and concepts you've mastered, ensuring you're prepared for more advanced topics.
Data Types
You've explored various Python data types, understanding how to use integers, strings, floats, and booleans effectively. These are crucial for handling different kinds of data in your programs.
Remember, we can determine the data type by using the type()
function:
item_name = "Milk" item_price = 3.14 item_quantity = 0 in_stock = False print(type(item_quantity)) # <class 'int'> print(type(item_name)) # <class 'str'> print(type(item_price)) # <class 'float'> print(type(in_stock)) # <class 'bool'>
Variable Naming Rules
Proper variable naming helps make your code readable and maintainable. You've learned to use meaningful names that reflect the data they hold and comply with Python's naming conventions.
Remember the rules:
1. Always Start with a Letter or an Underscore
Variable names must start with a letter or an underscore, like item_name
or _price
. Don't start a variable name with a number (e.g., 2item
is invalid).
2. Use Only Letters, Numbers, and Underscores
Variable names should only contain letters, numbers, and underscores. For example, item_name1
is fine, but avoid using special characters like dashes (item-name
is invalid).
3. Be Aware of Case Sensitivity
Python treats uppercase and lowercase letters as different. So, item
and Item
are two separate variables in Python.
4. Don't Use Python's Reserved Words
Avoid using Python's built-in keywords (like print
, if
, and type
) as variable names, as these are already used for special purposes in the language.
Storing Data in Variables
You've practiced storing and retrieving data using variables, a fundamental aspect of programming that allows for data manipulation.
You can review storing and retrieving data using variables using the example below:
# Storing data in variables item_price = 19.99 item_count = 5 total_cost = item_price * item_count print("Total cost: $", total_cost)
Using Variables
Using variables in calculations and functions has allowed you to perform dynamic operations based on variable data, as seen in the following example:
# Using variables to calculate discounts item_price = 19.99 discount_percentage = 0.2 discounted_price = item_price * (1 - discount_percentage) print("Discounted price: $", discounted_price)
String Indexing and Length
You've mastered how to access specific characters in a string and determine the length of a string using the len()
function, enhancing your ability to work with textual data. Remember, positive indexing starts at 0
, and negative indexing starts at -1
.
See the example below:
item_name = "Apples" print("First character:", item_name[0]) # A print("Last character:", item_name[-1]) # s print("Length of string:", len(item_name)) # 6
String Slicing and Concatenation
String slicing and concatenation have allowed you to manipulate strings effectively, creating new strings from existing ones. It's important to note that when the syntax string[start:end]
is used, start
is the index of the first character you want to include, and end
is the index one past the last character you want to include.
greeting = "Hello customer, welcome to the grocery store!" name = "Alice" personal_greeting = greeting[0:5] + " " + name + "," + greeting[15:] print(personal_greeting) # Hello Alice, welcome to the grocery store!
1. What is a data type in Python that is used to represent whole numbers?
2. Which of the following is a valid Python variable name?
3. What is the correct way to start a variable name in Python?
4. In Python, if you have the string fruit = "Apple"
, what does fruit[3]
return?
5. Given the string vegetables = "Carrot, Celery"
, how would you slice this string to only retrieve "Celery"
?
Takk for tilbakemeldingene dine!