Podcast
Questions and Answers
Which of the following variable types can store truthiness in Python?
Which of the following variable types can store truthiness in Python?
- String
- Boolean (correct)
- Floating Number
- Integer
Python requires you to explicitly declare the type of a variable before assigning a value to it.
Python requires you to explicitly declare the type of a variable before assigning a value to it.
False (B)
What is the primary use of the print()
function in Python?
What is the primary use of the print()
function in Python?
To display output
In Python, a ______ is an immutable sequence of elements.
In Python, a ______ is an immutable sequence of elements.
Match the following string methods with their corresponding actions:
Match the following string methods with their corresponding actions:
Which operator is used to find the remainder of a division operation in Python?
Which operator is used to find the remainder of a division operation in Python?
In Python, indentation is purely for readability and does not affect how the code is executed.
In Python, indentation is purely for readability and does not affect how the code is executed.
What is printed by the following Python Code? print(type(42.0))
What is printed by the following Python Code? print(type(42.0))
The ______
statement in a loop immediately terminates the loop's execution.
The ______
statement in a loop immediately terminates the loop's execution.
Given a list my_list = [10, 20, 30, 40]
, what will my_list[-1]
return?
Given a list my_list = [10, 20, 30, 40]
, what will my_list[-1]
return?
Flashcards
Print function
Print function
A function that displays output to the console.
String
String
Text characters enclosed in single or double quotes.
Variable
Variable
Stores data in a memory location which can be referenced via a name.
Integer
Integer
Signup and view all the flashcards
Float
Float
Signup and view all the flashcards
Boolean
Boolean
Signup and view all the flashcards
Input() function
Input() function
Signup and view all the flashcards
Concatenation
Concatenation
Signup and view all the flashcards
Type Conversion
Type Conversion
Signup and view all the flashcards
.find()
.find()
Signup and view all the flashcards
Study Notes
Introduction
- The lecture is for students in the beginning stages of programming or Python.
- Students can prepare for college placements, competitive coding, web development (Django), AI, machine learning, and data science after this video.
Requirements
- A laptop and excitement are needed.
Installation (Windows)
- Python 3.7 can be downloaded from python.org; the website link is in the notes.
- Install Python by opening and executing the downloaded file.
Running Python Code
- Use the
print
function to output statements. - Comments are highlighted lines not included in the code.
- Functions are code parts performing operations, written with parentheses
()
. - Text in Python is written in quotes.
Text Formatting
- Strings can use single or double quotes.
- Double quotes are preferred for strings (like Java/C++), and single quotes for characters.
- Click the green run button to run the code.
- Save code in a file (File -> Save As) with a
.py
extension (e.g.,first_prog.py
).
PyCharm Overview
- Used due to the virtual machine limitation.
- Install and open PyCharm by dragging it to the applications folder.
- Keep default settings while opening.
Creating a New Project (PyCharm)
- Open the software to see a window.
- Different projects can be created, each holding multiple Python files.
- Create a new project and name it (e.g., "College").
Project Location
- The path before the project name shows where project files will be saved.
- Choose an environment (can be ignored for beginners).
Base Interpreter
- The base interpreter (Python) can be chosen.
- The computer may have other Python versions.
- If options don't appear, directly create the project.
Creating a Python File
- Right-click, go to New, and select Python File.
- Name the file (e.g., "First Program").
- Code can be written in the opened file, named
first_prog.py
.
Introduction to Python Files
- Python files end with
.py
. - Files such as
d_txcindia.gov.in
might be related to Java-style extensions elsewhere. - Virtual environments store Python-specific files.
Focusing on a Single Python File
- Users can hide irrelevant files in environments with multiple Python files.
- Hiding files helps focus on the current coding task.
Outputting in Python
- The
print
function displays output. - Parentheses
()
followprint
, enclosing what to display. "Hello World"
example demonstrates printing a string.
Strings in Python
- Strings are text characters enclosed in double quotes.
- Strings are immutable text that remains as typed.
Case Sensitivity in Python
- Python is case-sensitive;
print
(lowercasep
) is different fromPrint
. - Incorrect case leads to errors because Python won't recognize undefined commands.
Functions like Print
- Parentheses signify
print
as a function. - Functions execute actions in Python; creation and use are covered later.
Running Code
- Run code via the "Run" option in the interface.
- The output appears in an output screen.
Keyboard Shortcuts
- Keyboard shortcuts like
Ctrl+Shift+R
speed up coding. - Shortcuts vary by OS; macOS has
Ctrl+Shift+R
, Windows has different ones.
Variables
- Variables store data in memory locations.
- Memory locations are assigned names, called variables, for easy reference.
Variable Naming and Storage
- Example:
name = "string"
stores a name. age = number
stores a number.
Printing Variables
- Printing a variable like
name
displays its stored value. - Quoting
name
would print "name" as a literal string.
Value Changing
- Variable values can be modified after initial assignment.
- Updated values are reflected in subsequent print statements.
Types of Variables
- String: Text inside quotes.
- Number: Integer (whole number).
- Floating Number: Number with decimal points (e.g., 24.0).
- Boolean: Represents truthiness (True or False).
Booleans
- Boolean variables are
True
orFalse
. - Case-sensitive:
True
must start with a capitalT
.
Python's Flexibility in Variable Declaration
- Python doesn't require pre-declaration of variable types.
- Variables adapt to the type of data assigned to them.
Variable Name Rules
- Naming conventions may allow underscores.
- Specific rules for variable naming are in the associated notes.
Exercise
- Define a person: first name "Tony," last name "Stark."
- Create an "age" variable as 51.
- Define an "is_genius" Boolean variable.
- Solution is available in the provided notes.
User Input
input()
function takes input from the user.- Example:
input("What is your name?")
.
Storing Input
- Store input in a variable:
name = input("What is your name?")
. - Use the stored variable to print:
print(name)
.
Concatenation
- Concatenate strings using the
+
operator. - Example:
"Hello " + name
combines two strings. - Concatenation joins strings together.
Superhero Exercise
- Prompt for a superhero name.
- Store the input.
- Print the entered name.
- Solution is in the notes.
Type Conversion
- Input is always received as a string.
- Conversion is needed for numerical operations.
Example - Old Age
- In the example,
"Enter your old age"
takes age as a string, stored as"Old Age"
. - New age calculation requires converting input to an integer.
Error Handling in Calculations
- Type error occurs when mixing strings and integers.
- Convert
"Old Age"
to an integer usingint()
for math operations.
Additional Conversions
- Use
float()
for floating-point numbers. - Use
str()
to convert to a string for printing. - Use
bool()
to convert to a bool variable.
Python Program Example - Summing Numbers
- Input: Receive two numbers from the user.
- Convert: Change from string to integer.
- Print: Print the resultant sum.
String Manipulation Operations
.upper()
: Converts the string to uppercase..lower()
: Converts strings to lowercase.- Use:
string.upper()
orstring.lower()
on the declared variable. - The original string is not modified.
.find()
strings
- Finds a string within a string and returns the first index.
- If the string cannot be found, it returns -1.
String Replacement
- Replaces parts or the whole string.
- Can replace characters.
Keyword in
- Used for true/false checks.
- Checks for the existence of a substring in a string.
Arithmetic Operators
+
for addition.-
for subtraction.*
for multiplication./
for division.//
for division to an integer.%
for remainder.
Power Operators
- Using
**
for number power.
Shortcuts
- Use operators and equal signs as shortcuts:
+=
,-=
,*=
, etc.
Order of Operations
- Standard operator precedence applies.
- Use parentheses to take priority over existing ordering.
Python Comments
- Used for not code - text documentation of code
Comparison Operators
- Used for true/false checks
- Compares for greater than, less than, equals, etc
Logical Operators
- Use for true/false conditionals, and, or, not.
If/Else Statements
- Use conditionals to perform tasks depending on the outcome of other tasks.
Colons
- Used for calling if statements
- Indentation to specify actions following the statement
If/Else examples
- Checks various scenarios such as age
Mini Project Notes - Calculator
- Used to calculate operations depending on the input of arguments
- Also performs the logic of error handling to ensure arguments are valid
Ranges
- Functions like defining the numbers 0 to 4
- Includes the starting values, but not the last values (0..4)
Loops (While)
- Use while statements to do an action until the arguments specified are false
For loops
- Loops through an object and its contents and performs an action once
Lists
- Use lists to store elements
- Lists have an index that starts at zero, such as 0, 1, 2, etc
- Elements do not need to be the same type
Negative Indexing
- Lists can be indexed in the negative: -1 refers to the last item in the list
List Operations
- Adding to the back = Appending
- Adding at the front = inserting at zero with
.insert(
- Check contents exist with
object in list
and is a true/false statement - Get the length with
len(list)
and print - Make the lists clear with
list.clear()
Break / Continue Statements
- Break commands stop a loop at an object
- Continue skips an object in an index
Tuples
- Tuples are immutable lists
- Cannot change the elements
Tuples Operation / Functions
- Can count specific elements:
tuple.count()
- Can use indexing:
tuples.index()
Sets
- Sets only support unique types
- Accessed out of order
Dictionaries
- Stores key-value pairs, for instance storing marks that matches subject
- To access an object type in the key:
dictionary[the key]
- To add a new key use:
dictionary[the key] = value
Functions
- Functions help to modularize code and perform tasks
Types of Functions
- Inbuild: int(), bool(), etc
- Module: Import math functions
- User-Defined - the programmer decides/creates them.
Function Example: add( , ) arguments
def print_sum(first,second):
is used to define two arguments Then use `print_sum(argument 1, argument 2 ) calls that function
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.