Computer science
  • Exam boards
    • OCR past papers >
      • Paper 1 J277 >
        • Paper1 (Sample) J277
        • Practice paper1 J277
      • Paper 2 J277 >
        • Paper 2 J277 Practice
    • Edexcel past papers >
      • 2020 Current spec (coming soon) >
        • Programming Language Subset (PLS)
      • Old 2016 SPEC >
        • Paper 1 (specimen1)
        • Paper 1 (specimen2)
        • Paper 1 (2018)
        • Paper 1 (2019)
        • Paper 2 (specimen1)
        • Paper 2 (2018)
        • Paper 2 (2019)
      • OLD 2013 SPEC >
        • Sample assessment 1 (old spec)
        • Sample assessment 2 (old spec)
        • June 2017 (old spec)
        • June 2016 (old spec)
        • June 2015 (old spec)
    • Paper 1 (by topic) >
      • Algorithms
      • Application software
      • Ascii
      • Binary
      • Cache
      • Client Server
      • Cloud
      • Compression
      • Computational thinking
      • CPU
      • Cyber security
      • Database
      • Fetch decode execute cycle
      • Embedded system
      • Encryption
      • File size
      • Flow chart
      • Hexadecimal
      • High Level language
      • ISP/Internet
      • Logic
      • Magnetic HDD
      • Networking
      • Open source
      • Operating system
      • Peer to peer
      • Images (Data representation)
      • Protocols
      • RAM
      • ROM
      • Run length encoding
      • Simulation software
      • Solid state HDD
      • Sound
      • Stored program concept
      • Utility software
    • Curriculum >
      • Binary
      • Hex
      • Data Representation
      • Compression
      • Encryption
      • What is a computer
      • How does a computer work
      • Hardware
      • Operating system
      • The CPU
      • Input devices
      • Data storage
      • Networks
      • Microcontrollers
      • Computational models
      • Emerging trends
      • Virtual machine
      • Models and simulators
      • Algorithms
    • Python >
      • Finding python
      • GCSE Python questions
      • Python coding >
        • Comments
        • Data types
        • Variables
        • If Else (Selection)
        • If Elif Else (Selection) >
          • Relational/Comparison Operator challenges
          • Selection challenges
        • Functions
        • For loop (iteration)
        • While loop (iteration) >
          • While loop challenges
        • Data structures >
          • Array challenges
          • 2D Array challenges
          • Looping thru 2D array
        • String slicing/indexing >
          • String slicing challenges
        • Subprograms >
          • Subprogram challenges
        • Local/global variable
        • Validation and try
        • methods
        • File I/O >
          • Files - Challenges
        • Pseudocode
        • Modules >
          • Turtle
        • Classes
        • NEA/Projects
  • KS3/Other
    • KS3 >
      • Coding >
        • GameMaker
        • HTML >
          • HTML tags
        • Scratch >
          • Scratch questions
        • Python >
          • Getting started
          • Functions
          • Variables
          • VariablesToo
          • inputs
          • Selection
          • Flowcharts
          • Data types
          • Pseudocode1
          • Pseudocode2
          • Subprogram1
          • Subprogram2
          • Coding challenges
      • Online Safety >
        • Screen time
        • Online Safety questions
      • IT >
        • Word
        • PowerPoint
      • Hardware >
        • All Hardware
        • Magnetic hard disk drive
        • Optical disk drive
        • Solid state drive
      • Logic >
        • CPU
        • AND gate
        • OR gate
        • NOT gate
        • Logical statements
      • Software
      • Binary/Hex/Algorithms >
        • Binary
        • Binary explained
        • Hexadecimal - 1 digit
        • Hexadecimal - 2 digits
        • Algorithms
        • ASCII
      • Networks
      • Protocols/Cyber attacks
    • BTEC CS
    • OCR A-Level >
      • 1.2 (Unit 2) >
        • Functions of an OS
        • Types of OS
        • Application generation
      • 1.2 (Unit 3) >
        • Software development
        • Algorithms
        • Programming paradigms
      • 1.3 (Unit 5) >
        • Networks
      • 1.5 (Unit 9) >
        • Laws and Acts
      • 2.3 (Unit 12) >
        • Analysis and design of algorithms
    • AQA AS-Level CS >
      • AS Curriculum >
        • Multiple choice questions
    • Business studies >
      • Keywords
      • BS Revision - Multiple choice
      • Paper 1 (9-1)
      • Paper 2 (9-1)
    • Junior Science >
      • 01 - Particles >
        • investigations and data
        • Equipment
        • Particles
      • 02 - Cells >
        • Microscope introduced
        • Looking at cells
        • Specialised cells
        • Unicellular organisms
      • 03 - Energy >
        • Energy introduced
        • Energy transfers
        • Conduction, Convection, Insulators
      • 04 - Chemical ideas & reactions >
        • Periodic table
        • Chemical reactions
        • Chemical reactions2
      • 05 - Light and Sound >
        • Light introduced
        • The Eye
        • Colours and filters
        • Waves and Sound
  • Contact/More
    • Code editor
    • Contact us
    • Microbit ideas
    • More
  • Self test
    • 01 Random topics
    • 02 Programming
    • 03 Data
    • 04 Computers
    • 05 Networks
    • 06 Legal/Ethics/Enviro'
    • Coding/Programming

Python coding

PYTHON DICTIONARY


Comments

Data types

Variables

If Else

If Elif Else

Functions

For loop

While loop

Data structures

String slicing/indexing

User defined function

Local/Global variable

Validation and Try

Methods

File I/O

Pseudocode

Modules​

What is a data string slicing or indexing?

A string is a data type. This can be some letters that make up a word or a sentence or some numbers that make up for example, a telephone number. A string always has speech marks around them. Take the string "banana", each letter in the word banana exists in a specific location. In Python, we would say that the letter "b" in "banana" exists in location zero. i.e. the index is the hidden position of each character in the string that always starts with zero.
Picture

Picture

Python challenge - String slicing

​Enter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Picture
How do I display the middle "d" in the word "MAdAM"? The code above allows you to slice it out and display it. Pay attention to the index values [2]:[3]. This is providing more detail to exactly how the index is labelled. Much like the example of "banana" above, the labels actually exist between each letter. So, in this example, the letter "d" exists between index location 2 and index location 3.

More string slicing

Run the code below and see what it does
Changes the names (and rerun)
Change the [0] to [1], guess what will happen then rerun the code
Change the [0] to [0:2], guess what will happen then rerun the code
​Edit the code to allow anyone to enter their name and it displays their initials
​

Concatenation explained

Question: When is an add sign + not an add sign?
Answer: When you are NOT working with numbers!
Explanation: in line 1 and 2 below, "int" is being used (casted), this is short for integer (a whole number)
In line 4, the numbers are being added together with the +
However, in line 6 and 7 "Str" (Strings) are being used (casted)
In line 10, the strings are being concatenated (squashed together)
This is an example of polymorphism (the + sign behaving in two different ways, adding or squashing!)


Python challenge - Slicing and loops

Enter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Picture
What do you thin the following code will do? Try it out. Now change the "c" to a "d". What if you change the [0] to [1] and change the "c" to an "a".

Test your understanding

Loading String slicing

The len function - len()

Before you run the code below, know one thing. To "spam" a keyboard means to tap your fingers as quickly as you can on any letters or numbers (apart from enter). Who doesn't love to spam a keyboard?
​

More len() function

What do you think the following code will do? Try it out. Now add a new date (pretend that France won the world cup in 1994 by adding the date to list on line 3 ) What is output down when you run  he code?
​

​Test your understanding

Loading len() function

Click here for string slicing challenges

The .find() method

​What do you think the following code will do? Try it out. Now change the target to "am" and run it again. What is output down when you run the code?
​

​​Test your understanding

Loading .find() method

The .isalpha() method

​What do you think the following code will do? Try it out. Now delete the numbers and set name to "Barcelona". run it again
​

The .isdigit() method

What do you think the following code will do? Try it out. Now delete the random letter hidden between the zeros. Run it again.


More methods
Home
Contact
  • Exam boards
    • OCR past papers >
      • Paper 1 J277 >
        • Paper1 (Sample) J277
        • Practice paper1 J277
      • Paper 2 J277 >
        • Paper 2 J277 Practice
    • Edexcel past papers >
      • 2020 Current spec (coming soon) >
        • Programming Language Subset (PLS)
      • Old 2016 SPEC >
        • Paper 1 (specimen1)
        • Paper 1 (specimen2)
        • Paper 1 (2018)
        • Paper 1 (2019)
        • Paper 2 (specimen1)
        • Paper 2 (2018)
        • Paper 2 (2019)
      • OLD 2013 SPEC >
        • Sample assessment 1 (old spec)
        • Sample assessment 2 (old spec)
        • June 2017 (old spec)
        • June 2016 (old spec)
        • June 2015 (old spec)
    • Paper 1 (by topic) >
      • Algorithms
      • Application software
      • Ascii
      • Binary
      • Cache
      • Client Server
      • Cloud
      • Compression
      • Computational thinking
      • CPU
      • Cyber security
      • Database
      • Fetch decode execute cycle
      • Embedded system
      • Encryption
      • File size
      • Flow chart
      • Hexadecimal
      • High Level language
      • ISP/Internet
      • Logic
      • Magnetic HDD
      • Networking
      • Open source
      • Operating system
      • Peer to peer
      • Images (Data representation)
      • Protocols
      • RAM
      • ROM
      • Run length encoding
      • Simulation software
      • Solid state HDD
      • Sound
      • Stored program concept
      • Utility software
    • Curriculum >
      • Binary
      • Hex
      • Data Representation
      • Compression
      • Encryption
      • What is a computer
      • How does a computer work
      • Hardware
      • Operating system
      • The CPU
      • Input devices
      • Data storage
      • Networks
      • Microcontrollers
      • Computational models
      • Emerging trends
      • Virtual machine
      • Models and simulators
      • Algorithms
    • Python >
      • Finding python
      • GCSE Python questions
      • Python coding >
        • Comments
        • Data types
        • Variables
        • If Else (Selection)
        • If Elif Else (Selection) >
          • Relational/Comparison Operator challenges
          • Selection challenges
        • Functions
        • For loop (iteration)
        • While loop (iteration) >
          • While loop challenges
        • Data structures >
          • Array challenges
          • 2D Array challenges
          • Looping thru 2D array
        • String slicing/indexing >
          • String slicing challenges
        • Subprograms >
          • Subprogram challenges
        • Local/global variable
        • Validation and try
        • methods
        • File I/O >
          • Files - Challenges
        • Pseudocode
        • Modules >
          • Turtle
        • Classes
        • NEA/Projects
  • KS3/Other
    • KS3 >
      • Coding >
        • GameMaker
        • HTML >
          • HTML tags
        • Scratch >
          • Scratch questions
        • Python >
          • Getting started
          • Functions
          • Variables
          • VariablesToo
          • inputs
          • Selection
          • Flowcharts
          • Data types
          • Pseudocode1
          • Pseudocode2
          • Subprogram1
          • Subprogram2
          • Coding challenges
      • Online Safety >
        • Screen time
        • Online Safety questions
      • IT >
        • Word
        • PowerPoint
      • Hardware >
        • All Hardware
        • Magnetic hard disk drive
        • Optical disk drive
        • Solid state drive
      • Logic >
        • CPU
        • AND gate
        • OR gate
        • NOT gate
        • Logical statements
      • Software
      • Binary/Hex/Algorithms >
        • Binary
        • Binary explained
        • Hexadecimal - 1 digit
        • Hexadecimal - 2 digits
        • Algorithms
        • ASCII
      • Networks
      • Protocols/Cyber attacks
    • BTEC CS
    • OCR A-Level >
      • 1.2 (Unit 2) >
        • Functions of an OS
        • Types of OS
        • Application generation
      • 1.2 (Unit 3) >
        • Software development
        • Algorithms
        • Programming paradigms
      • 1.3 (Unit 5) >
        • Networks
      • 1.5 (Unit 9) >
        • Laws and Acts
      • 2.3 (Unit 12) >
        • Analysis and design of algorithms
    • AQA AS-Level CS >
      • AS Curriculum >
        • Multiple choice questions
    • Business studies >
      • Keywords
      • BS Revision - Multiple choice
      • Paper 1 (9-1)
      • Paper 2 (9-1)
    • Junior Science >
      • 01 - Particles >
        • investigations and data
        • Equipment
        • Particles
      • 02 - Cells >
        • Microscope introduced
        • Looking at cells
        • Specialised cells
        • Unicellular organisms
      • 03 - Energy >
        • Energy introduced
        • Energy transfers
        • Conduction, Convection, Insulators
      • 04 - Chemical ideas & reactions >
        • Periodic table
        • Chemical reactions
        • Chemical reactions2
      • 05 - Light and Sound >
        • Light introduced
        • The Eye
        • Colours and filters
        • Waves and Sound
  • Contact/More
    • Code editor
    • Contact us
    • Microbit ideas
    • More
  • Self test
    • 01 Random topics
    • 02 Programming
    • 03 Data
    • 04 Computers
    • 05 Networks
    • 06 Legal/Ethics/Enviro'
    • Coding/Programming